use std::time::Instant;
#[derive(Debug, Clone)]
pub struct KeepAliveStatistics {
pub total_connections_created: usize,
pub total_connections_refreshed: usize,
pub total_stale_detected: usize,
pub total_proactive_pings: usize,
pub total_ping_failures: usize,
pub active_connections: usize,
pub avg_time_saved_ms: u64,
pub last_activity: Instant,
}
impl Default for KeepAliveStatistics {
fn default() -> Self {
Self {
total_connections_created: 0,
total_connections_refreshed: 0,
total_stale_detected: 0,
total_proactive_pings: 0,
total_ping_failures: 0,
active_connections: 0,
avg_time_saved_ms: 0,
last_activity: Instant::now(),
}
}
}
impl KeepAliveStatistics {
pub fn update_activity(&mut self) {
self.last_activity = Instant::now();
}
pub fn inc_connections_created(&mut self) {
self.total_connections_created += 1;
self.active_connections += 1;
self.update_activity();
}
pub fn inc_connections_refreshed(&mut self) {
self.total_connections_refreshed += 1;
}
pub fn inc_stale_detected(&mut self) {
self.total_stale_detected += 1;
}
pub fn dec_active_connections(&mut self) {
if self.active_connections > 0 {
self.active_connections -= 1;
}
}
pub fn ping_success_rate(&self) -> f64 {
let total = self.total_proactive_pings + self.total_ping_failures;
if total == 0 {
1.0
} else {
self.total_proactive_pings as f64 / total as f64
}
}
}