use crate::jump::connection::JumpHostConnection;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, info};
pub(super) async fn cleanup_connections(
connections: &RwLock<Vec<Arc<JumpHostConnection>>>,
max_idle_time: Duration,
max_connection_age: Duration,
) {
let mut connections = connections.write().await;
let mut to_remove = Vec::new();
for (i, conn) in connections.iter().enumerate() {
let should_remove = !conn.is_alive().await
|| conn.idle_time().await > max_idle_time
|| conn.age() > max_connection_age;
if should_remove {
to_remove.push(i);
debug!(
"Removing stale connection to {:?} (age: {:?}, idle: {:?})",
conn.destination,
conn.age(),
conn.idle_time().await
);
}
}
for i in to_remove.iter().rev() {
connections.remove(*i);
}
if !to_remove.is_empty() {
info!("Cleaned up {} stale connections", to_remove.len());
}
}
pub(super) async fn get_active_connection_count(
connections: &RwLock<Vec<Arc<JumpHostConnection>>>,
) -> usize {
let connections = connections.read().await;
connections.len()
}
pub(super) async fn cleanup_all(connections: &RwLock<Vec<Arc<JumpHostConnection>>>) {
let mut connections = connections.write().await;
connections.clear();
debug!("Cleaned up jump host connection cache");
}