@@ -61,6 +61,7 @@ pub(crate) struct Connection {
61
61
/// currently checked into the pool, this will be None.
62
62
pub ( super ) pool : Option < Weak < ConnectionPoolInner > > ,
63
63
64
+ commmand_executing : bool ,
64
65
stream : AsyncStream ,
65
66
66
67
#[ derivative( Debug = "ignore" ) ]
@@ -85,6 +86,7 @@ impl Connection {
85
86
id,
86
87
generation,
87
88
pool : None ,
89
+ commmand_executing : false ,
88
90
ready_and_available_time : None ,
89
91
stream : AsyncStream :: connect ( stream_options) . await ?,
90
92
address,
@@ -206,11 +208,17 @@ impl Connection {
206
208
command : Command ,
207
209
request_id : impl Into < Option < i32 > > ,
208
210
) -> Result < CommandResponse > {
211
+ self . commmand_executing = true ;
212
+
209
213
let message = Message :: with_command ( command, request_id. into ( ) ) ;
210
214
message. write_to ( & mut self . stream ) . await ?;
211
215
212
216
let response_message = Message :: read_from ( & mut self . stream ) . await ?;
213
- CommandResponse :: new ( self . address . clone ( ) , response_message)
217
+ let command_response = CommandResponse :: new ( self . address . clone ( ) , response_message) ;
218
+
219
+ self . commmand_executing = false ;
220
+
221
+ command_response
214
222
}
215
223
216
224
/// Gets the connection's StreamDescription.
@@ -253,24 +261,30 @@ impl Connection {
253
261
254
262
impl Drop for Connection {
255
263
fn drop ( & mut self ) {
256
- // If the connection has a weak reference to a pool, that means that the connection is being
257
- // dropped when it's checked out. If the pool is still alive, it should check itself back
258
- // in. Otherwise, the connection should close itself and emit a ConnectionClosed event
259
- // (because the `close_and_drop` helper was not called explicitly).
260
- //
261
- // If the connection does not have a weak reference to a pool, then the connection is being
262
- // dropped while it's not checked out. This means that the pool called the `close_and_drop`
263
- // helper explicitly, so we don't add it back to the pool or emit any events.
264
- if let Some ( ref weak_pool_ref) = self . pool {
265
- if let Some ( strong_pool_ref) = weak_pool_ref. upgrade ( ) {
266
- let dropped_connection_state = self . take ( ) ;
267
- RUNTIME . execute ( async move {
268
- strong_pool_ref
269
- . check_in ( dropped_connection_state. into ( ) )
270
- . await ;
271
- } ) ;
272
- } else {
273
- self . close ( ConnectionClosedReason :: PoolClosed ) ;
264
+ if self . commmand_executing {
265
+ self . close ( ConnectionClosedReason :: Dropped ) ;
266
+ } else {
267
+ // If the connection has a weak reference to a pool, that means that the connection is
268
+ // being dropped when it's checked out. If the pool is still alive, it
269
+ // should check itself back in. Otherwise, the connection should close
270
+ // itself and emit a ConnectionClosed event (because the `close_and_drop`
271
+ // helper was not called explicitly).
272
+ //
273
+ // If the connection does not have a weak reference to a pool, then the connection is
274
+ // being dropped while it's not checked out. This means that the pool called
275
+ // the `close_and_drop` helper explicitly, so we don't add it back to the
276
+ // pool or emit any events.
277
+ if let Some ( ref weak_pool_ref) = self . pool {
278
+ if let Some ( strong_pool_ref) = weak_pool_ref. upgrade ( ) {
279
+ let dropped_connection_state = self . take ( ) ;
280
+ RUNTIME . execute ( async move {
281
+ strong_pool_ref
282
+ . check_in ( dropped_connection_state. into ( ) )
283
+ . await ;
284
+ } ) ;
285
+ } else {
286
+ self . close ( ConnectionClosedReason :: PoolClosed ) ;
287
+ }
274
288
}
275
289
}
276
290
}
@@ -318,6 +332,7 @@ impl From<DroppedConnectionState> for Connection {
318
332
id : state. id ,
319
333
address : state. address . clone ( ) ,
320
334
generation : state. generation ,
335
+ commmand_executing : false ,
321
336
stream : std:: mem:: replace ( & mut state. stream , AsyncStream :: Null ) ,
322
337
handler : state. handler . take ( ) ,
323
338
stream_description : state. stream_description . take ( ) ,
0 commit comments