isahc/agent/
timer.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
35
36
37
use std::time::{Duration, Instant};

use crossbeam_utils::atomic::AtomicCell;

#[derive(Debug)]
pub(crate) struct Timer {
    timeout: AtomicCell<Option<Instant>>,
}

impl Timer {
    pub(crate) fn new() -> Self {
        Self {
            timeout: AtomicCell::new(None),
        }
    }

    pub(crate) fn is_expired(&self, now: Instant) -> bool {
        self.timeout
            .load()
            .map(|timeout| now >= timeout)
            .unwrap_or(false)
    }

    pub(crate) fn get_remaining(&self, now: Instant) -> Option<Duration> {
        self.timeout
            .load()
            .map(|timeout| timeout.saturating_duration_since(now))
    }

    pub(crate) fn start(&self, timeout: Duration) {
        self.timeout.store(Some(Instant::now() + timeout));
    }

    pub(crate) fn stop(&self) {
        self.timeout.store(None);
    }
}