use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
lazy_static! {
pub static ref METRICS: InternalMetrics = InternalMetrics::default();
}
pub struct InternalMetrics {
pub active_sessions: AtomicUsize,
pub moving_window: u64,
pub unsolicited_requests_per_window: AtomicUsize,
pub bytes_sent: AtomicUsize,
pub bytes_recv: AtomicUsize,
pub ipv4_contactable: AtomicBool,
pub ipv6_contactable: AtomicBool,
}
impl Default for InternalMetrics {
fn default() -> Self {
InternalMetrics {
moving_window: 5,
active_sessions: AtomicUsize::new(0),
unsolicited_requests_per_window: AtomicUsize::new(0),
bytes_sent: AtomicUsize::new(0),
bytes_recv: AtomicUsize::new(0),
ipv4_contactable: AtomicBool::new(false),
ipv6_contactable: AtomicBool::new(false),
}
}
}
impl InternalMetrics {
pub fn add_recv_bytes(&self, bytes: usize) {
let current_bytes_recv = self.bytes_recv.load(Ordering::Relaxed);
self.bytes_recv
.store(current_bytes_recv.saturating_add(bytes), Ordering::Relaxed);
}
pub fn add_sent_bytes(&self, bytes: usize) {
let current_bytes_sent = self.bytes_sent.load(Ordering::Relaxed);
self.bytes_sent
.store(current_bytes_sent.saturating_add(bytes), Ordering::Relaxed);
}
}
#[derive(Clone, Debug)]
pub struct Metrics {
pub active_sessions: usize,
pub unsolicited_requests_per_second: f64,
pub bytes_sent: usize,
pub bytes_recv: usize,
pub ipv4_contactable: bool,
pub ipv6_contactable: bool,
}
impl From<&METRICS> for Metrics {
fn from(internal_metrics: &METRICS) -> Self {
Metrics {
active_sessions: internal_metrics.active_sessions.load(Ordering::Relaxed),
unsolicited_requests_per_second: internal_metrics
.unsolicited_requests_per_window
.load(Ordering::Relaxed) as f64
/ internal_metrics.moving_window as f64,
bytes_sent: internal_metrics.bytes_sent.load(Ordering::Relaxed),
bytes_recv: internal_metrics.bytes_recv.load(Ordering::Relaxed),
ipv4_contactable: internal_metrics.ipv4_contactable.load(Ordering::Relaxed),
ipv6_contactable: internal_metrics.ipv6_contactable.load(Ordering::Relaxed),
}
}
}