@@ -23,7 +23,7 @@ use std::sync::atomic::AtomicBool;
2323use std:: sync:: atomic:: Ordering ;
2424use std:: sync:: Arc ;
2525use std:: sync:: RwLock ;
26- use std:: time:: Duration ;
26+ use std:: time:: { Duration , Instant } ;
2727
2828use crate :: pool:: AsyncPool ;
2929
@@ -241,8 +241,13 @@ pub(crate) fn spawn_size_stat_collector(
241241/// Reap connections that are too old (older than 30 minutes) or if there
242242/// are more than `connection_min_idle` connections in the pool that have
243243/// been idle for longer than `idle_timeout`
244- pub ( crate ) fn spawn_connection_reaper ( pool : AsyncPool , idle_timeout : Duration ) {
244+ pub ( crate ) fn spawn_connection_reaper (
245+ pool : AsyncPool ,
246+ idle_timeout : Duration ,
247+ wait_gauge : Option < Gauge > ,
248+ ) {
245249 const MAX_LIFETIME : Duration = Duration :: from_secs ( 30 * 60 ) ;
250+ const CHECK_INTERVAL : Duration = Duration :: from_secs ( 30 ) ;
246251 let Some ( min_idle) = ENV_VARS . store . connection_min_idle else {
247252 // If this is None, we will never reap anything
248253 return ;
@@ -254,7 +259,9 @@ pub(crate) fn spawn_connection_reaper(pool: AsyncPool, idle_timeout: Duration) {
254259 tokio:: task:: spawn ( async move {
255260 loop {
256261 let mut idle_count = 0 ;
262+ let mut last_used = Instant :: now ( ) ;
257263 pool. retain ( |_, metrics| {
264+ last_used = last_used. min ( metrics. recycled . unwrap_or ( metrics. created ) ) ;
258265 if metrics. age ( ) > MAX_LIFETIME {
259266 return false ;
260267 }
@@ -264,13 +271,18 @@ pub(crate) fn spawn_connection_reaper(pool: AsyncPool, idle_timeout: Duration) {
264271 }
265272 true
266273 } ) ;
267- tokio:: time:: sleep ( Duration :: from_secs ( 30 ) ) . await ;
274+ if last_used. elapsed ( ) > CHECK_INTERVAL {
275+ // Reset wait time if there was no activity recently so that
276+ // we don't report stale wait times
277+ wait_gauge. as_ref ( ) . map ( |wait_gauge| wait_gauge. set ( 0.0 ) ) ;
278+ }
279+ tokio:: time:: sleep ( CHECK_INTERVAL ) . await ;
268280 }
269281 } ) ;
270282}
271283
272284pub ( crate ) struct WaitMeter {
273- wait_gauge : Gauge ,
285+ pub ( crate ) wait_gauge : Gauge ,
274286 pub ( crate ) wait_stats : PoolWaitStats ,
275287}
276288
0 commit comments