From 726683a4d85debff071df8783a10ac0b2d303caa Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Thu, 17 Dec 2020 21:57:03 -0800 Subject: [PATCH 1/2] decrement pool size when connection is released --- sqlx-core/src/pool/connection.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/pool/connection.rs b/sqlx-core/src/pool/connection.rs index 891299ef90..9bde08ca29 100644 --- a/sqlx-core/src/pool/connection.rs +++ b/sqlx-core/src/pool/connection.rs @@ -56,9 +56,13 @@ impl DerefMut for PoolConnection { } impl PoolConnection { - // explicitly release a connection from the pool + /// Explicitly release a connection from the pool pub fn release(mut self) -> DB::Connection { - self.live.take().expect("PoolConnection double-dropped").raw + self.live + .take() + .expect("PoolConnection double-dropped") + .float(&self.pool) + .detach() } } @@ -154,6 +158,10 @@ impl<'s, DB: Database> Floating<'s, Live> { } } + pub fn detach(self) -> DB::Connection { + self.inner.raw + } + pub fn into_idle(self) -> Floating<'s, Idle> { Floating { inner: self.inner.into_idle(), From d74b7e0ef4d5a8249950118d425553075e866422 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Thu, 17 Dec 2020 22:02:08 -0800 Subject: [PATCH 2/2] manually close connections in SharedPool::close --- sqlx-core/src/pool/inner.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sqlx-core/src/pool/inner.rs b/sqlx-core/src/pool/inner.rs index aef5983e8a..8e1286597d 100644 --- a/sqlx-core/src/pool/inner.rs +++ b/sqlx-core/src/pool/inner.rs @@ -47,10 +47,11 @@ impl SharedPool { // ensure we wait until the pool is actually closed while self.size() > 0 { - let _ = self - .idle_conns - .pop() - .map(|idle| Floating::from_idle(idle, self)); + if let Ok(idle) = self.idle_conns.pop() { + if let Err(e) = Floating::from_idle(idle, self).close().await { + log::warn!("error occurred while closing the pool connection: {}", e); + } + } // yield to avoid starving the executor sqlx_rt::yield_now().await;