@@ -95,7 +95,7 @@ impl Worker {
95
95
Poll :: Ready ( Some ( request) ) => Poll :: Ready ( request) ,
96
96
Poll :: Ready ( None ) => {
97
97
// Channel was closed, explicitly or because the sender was dropped. Either way
98
- // we should start a gracefull shutdown.
98
+ // we should start a graceful shutdown.
99
99
self . socket
100
100
. write_buffer_mut ( )
101
101
. put_slice ( & [ Terminate :: FORMAT as u8 , 0 , 0 , 0 , 4 ] ) ;
@@ -151,6 +151,8 @@ impl Worker {
151
151
while let Poll :: Ready ( response) = self . poll_next_message ( cx) ? {
152
152
match response. format {
153
153
BackendMessageFormat :: ReadyForQuery => {
154
+ // Cloning a `ReceivedMessage` here is cheap because it only clones the
155
+ // underlying `Bytes`
154
156
let rfq: ReadyForQuery = response. clone ( ) . decode ( ) ?;
155
157
self . shared . set_transaction_status ( rfq. transaction_status ) ;
156
158
@@ -187,6 +189,7 @@ impl Worker {
187
189
}
188
190
189
191
if self . state != WorkerState :: Open && self . back_log . is_empty ( ) {
192
+ // After the connection is closed and the backlog is empty we can close the socket.
190
193
self . state = WorkerState :: Closed ;
191
194
}
192
195
Ok ( ( ) )
@@ -232,18 +235,15 @@ impl Worker {
232
235
#[ inline( always) ]
233
236
fn poll_shutdown ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) > > {
234
237
if self . state == WorkerState :: Closed {
235
- // The buffer is closed, a [Terminate] message has been sent, now try and close the socket.
238
+ // The channel is closed, all requests are flushed and a [Terminate] message has been
239
+ // sent, now try and close the socket
236
240
self . socket . poll_close_unpin ( cx)
237
241
} else {
238
242
Poll :: Pending
239
243
}
240
244
}
241
- }
242
-
243
- impl Future for Worker {
244
- type Output = Result < ( ) > ;
245
245
246
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
246
+ fn poll_worker ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) > > {
247
247
// Try to receive responses from the database and handle them.
248
248
self . poll_backlog ( cx) ?;
249
249
@@ -260,6 +260,17 @@ impl Future for Worker {
260
260
}
261
261
}
262
262
263
+ impl Future for Worker {
264
+ type Output = Result < ( ) > ;
265
+
266
+ fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
267
+ self . poll_worker ( cx) . map_err ( |e| {
268
+ tracing:: error!( "Background worker stopped with error: {e:?}" ) ;
269
+ e
270
+ } )
271
+ }
272
+ }
273
+
263
274
#[ derive( Clone ) ]
264
275
pub struct Shared ( Arc < Mutex < SharedInner > > ) ;
265
276
0 commit comments