monoio_client/client/
metrics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::time::{Duration, Instant};

/// Statistics regarding an connection returned by the pool
#[derive(Clone, Copy, Debug)]
#[must_use]
pub struct Metrics {
    /// The instant when this connection was created
    pub created: Instant,
    /// The instant when this connection was last used
    pub reused: Option<Instant>,
    /// The number of times the connections was reused
    pub reuse_count: usize,
}

impl Metrics {
    /// Access the age of this connection
    pub fn age(&self) -> Duration {
        self.created.elapsed()
    }
    /// Get the time elapsed when this connection was last used
    pub fn last_used(&self) -> Duration {
        self.reused.unwrap_or(self.created).elapsed()
    }
}

impl Default for Metrics {
    fn default() -> Self {
        Self {
            created: Instant::now(),
            reused: None,
            reuse_count: 0,
        }
    }
}