Files
nx9-chronoseal-rs/server/src/cleanup.rs
T
2026-06-04 19:58:21 +05:30

30 lines
1.1 KiB
Rust

use crate::session::AppState;
use std::sync::Arc;
/// Runs an infinite background loop that periodically cleans up database and memory resources.
///
/// Every 60 seconds, this loop performs two tasks:
/// 1. Evicts expired session records from the configured database storage backend.
/// 2. Evicts stale rate-limiter entries that have outlived the current rate-limiting window.
///
/// # Arguments
/// * `state` - Shared reference to the server application state.
pub async fn cleanup_loop(state: Arc<AppState>) {
loop {
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
// Evict expired sessions from the configured storage backend.
{
if let Err(err) = state.db_pool.delete_expired_sessions() {
tracing::error!("Failed to evict expired sessions: {}", err);
}
}
// Evict stale rate-limiter entries to prevent unbounded map growth.
{
let window_secs = state.get_config().rate_limit_window_secs;
state.rate_limiter.evict_stale(window_secs);
}
}
}