subms_rate_limiter/features/
clock.rs1use std::sync::Mutex;
6use std::time::Instant;
7
8pub trait Clock: Send + Sync {
11 fn now_ns(&self) -> u64;
13}
14
15pub struct SystemClock {
18 origin: Instant,
19}
20
21impl SystemClock {
22 pub fn new() -> Self {
23 Self {
24 origin: Instant::now(),
25 }
26 }
27}
28
29impl Default for SystemClock {
30 fn default() -> Self {
31 Self::new()
32 }
33}
34
35impl Clock for SystemClock {
36 fn now_ns(&self) -> u64 {
37 self.origin.elapsed().as_nanos() as u64
38 }
39}
40
41pub struct TestClock {
44 now: Mutex<u64>,
45}
46
47impl TestClock {
48 pub fn new() -> Self {
49 Self { now: Mutex::new(0) }
50 }
51
52 pub fn with_start(start_ns: u64) -> Self {
53 Self {
54 now: Mutex::new(start_ns),
55 }
56 }
57
58 pub fn advance(&self, ns: u64) {
59 let mut g = self.now.lock().unwrap();
60 *g = g.saturating_add(ns);
61 }
62
63 pub fn advance_ms(&self, ms: u64) {
64 self.advance(ms.saturating_mul(1_000_000));
65 }
66}
67
68impl Default for TestClock {
69 fn default() -> Self {
70 Self::new()
71 }
72}
73
74impl Clock for TestClock {
75 fn now_ns(&self) -> u64 {
76 *self.now.lock().unwrap()
77 }
78}
79
80#[cfg(test)]
81#[path = "clock_tests.rs"]
82mod tests;