Skip to content

Commit 4d4e007

Browse files
Stephan Dillysaghm
authored andcommitted
RUST-530 Decrement connection count when connection is dropped due to unfinished operation
1 parent ffa0b66 commit 4d4e007

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/cmap/conn/mod.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -263,30 +263,34 @@ impl Connection {
263263

264264
impl Drop for Connection {
265265
fn drop(&mut self) {
266-
if self.command_executing {
267-
self.close(ConnectionClosedReason::Dropped);
268-
} else {
269-
// If the connection has a weak reference to a pool, that means that the connection is
270-
// being dropped when it's checked out. If the pool is still alive, it
271-
// should check itself back in. Otherwise, the connection should close
272-
// itself and emit a ConnectionClosed event (because the `close_and_drop`
273-
// helper was not called explicitly).
274-
//
275-
// If the connection does not have a weak reference to a pool, then the connection is
276-
// being dropped while it's not checked out. This means that the pool called
277-
// the `close_and_drop` helper explicitly, so we don't add it back to the
278-
// pool or emit any events.
279-
if let Some(ref weak_pool_ref) = self.pool {
280-
if let Some(strong_pool_ref) = weak_pool_ref.upgrade() {
281-
let dropped_connection_state = self.take();
282-
RUNTIME.execute(async move {
266+
let command_executing = self.command_executing;
267+
268+
// If the connection has a weak reference to a pool, that means that the connection is
269+
// being dropped when it's checked out. If the pool is still alive, it
270+
// should check itself back in. Otherwise, the connection should close
271+
// itself and emit a ConnectionClosed event (because the `close_and_drop`
272+
// helper was not called explicitly).
273+
//
274+
// If the connection does not have a weak reference to a pool, then the connection is
275+
// being dropped while it's not checked out. This means that the pool called
276+
// the `close_and_drop` helper explicitly, so we don't add it back to the
277+
// pool or emit any events.
278+
if let Some(ref weak_pool_ref) = self.pool {
279+
if let Some(strong_pool_ref) = weak_pool_ref.upgrade() {
280+
let dropped_connection_state = self.take();
281+
RUNTIME.execute(async move {
282+
if command_executing {
283+
strong_pool_ref
284+
.dropped(dropped_connection_state.into())
285+
.await;
286+
} else {
283287
strong_pool_ref
284288
.check_in(dropped_connection_state.into())
285289
.await;
286-
});
287-
} else {
288-
self.close(ConnectionClosedReason::PoolClosed);
289-
}
290+
}
291+
});
292+
} else {
293+
self.close(ConnectionClosedReason::PoolClosed);
290294
}
291295
}
292296
}

src/cmap/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ impl ConnectionPoolInner {
300300
}
301301
}
302302

303+
async fn dropped(&self, conn: Connection) {
304+
let mut connection_manager = self.connection_manager.lock().await;
305+
306+
connection_manager.close_connection(conn, ConnectionClosedReason::Dropped);
307+
308+
self.wait_queue.wake_front();
309+
}
310+
303311
async fn check_in(&self, mut conn: Connection) {
304312
self.emit_event(|handler| {
305313
handler.handle_connection_checked_in_event(conn.checked_in_event());

0 commit comments

Comments
 (0)