Skip to content

Commit 5bd2a89

Browse files
committed
Close Redis connections idle for > 5 minutes
1 parent 09d0eae commit 5bd2a89

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

apps/labrinth/src/database/redis.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,25 @@ impl RedisPool {
6565
.build()
6666
.expect("Redis connection failed");
6767

68-
RedisPool {
68+
let pool = RedisPool {
6969
url,
7070
pool,
7171
meta_namespace: meta_namespace.unwrap_or("".to_string()),
72-
}
72+
};
73+
74+
let interval = Duration::from_secs(30);
75+
let max_age = Duration::from_secs(5 * 60); // 5 minutes
76+
let pool_ref = pool.clone();
77+
tokio::spawn(async move {
78+
loop {
79+
tokio::time::sleep(interval).await;
80+
pool_ref
81+
.pool
82+
.retain(|_, metrics| metrics.last_used() < max_age);
83+
}
84+
});
85+
86+
pool
7387
}
7488

7589
pub async fn register_and_set_metrics(

0 commit comments

Comments
 (0)