From a843cf739e2f80ddf8bdb4f2111c795f61ca5513 Mon Sep 17 00:00:00 2001 From: Taddes Date: Wed, 4 Dec 2024 20:36:09 -0500 Subject: [PATCH] add pool sweeper task --- syncstorage-spanner/src/pool.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/syncstorage-spanner/src/pool.rs b/syncstorage-spanner/src/pool.rs index 6ebe87cbef..b542112c5a 100644 --- a/syncstorage-spanner/src/pool.rs +++ b/syncstorage-spanner/src/pool.rs @@ -1,5 +1,6 @@ use std::{collections::HashMap, fmt, sync::Arc, time::Duration}; +use actix_web::rt; use async_trait::async_trait; use syncserver_common::{BlockingThreadpool, Metrics}; use syncserver_db_common::{GetPoolState, PoolState}; @@ -84,6 +85,22 @@ impl SpannerDbPool { self.quota, )) } + + /// Spawn a task to periodically evict idle connections. Calls wrapper sweeper fn + /// to use pool.retain, retaining objects only if they are shorter in duration than + /// defined max_idle. + pub fn spawn_sweeper(&self, interval: Duration) { + let Some(max_idle) = self.pool.manager().settings.max_lifespan else { + return; + }; + let pool = self.pool.clone(); + rt::spawn(async move { + loop { + sweeper(&pool, max_idle); + rt::time::sleep(interval).await; + } + }); + } } /// Sweeper to retain only the objects specified within the closure.