1use std::time::Instant;
2use tracing::{debug, warn};
3
4#[derive(Debug, Clone)]
6pub struct GlobalTimer {
7 pub base_timestamp: i64,
9 pub base_instant: Instant,
11 pub last_sync_at: Instant,
13 pub initialized: bool,
15}
16
17impl GlobalTimer {
18 pub fn new(initial_timestamp: i64) -> Self {
20 let base_instant = Instant::now();
21 debug!("初始化全局计时器,时间戳: {}", initial_timestamp);
22
23 Self {
24 base_timestamp: initial_timestamp,
25 base_instant,
26 last_sync_at: base_instant,
27 initialized: true,
28 }
29 }
30
31 pub fn get_current_timestamp(&self) -> i64 {
33 let elapsed_secs = self.base_instant.elapsed().as_secs() as i64;
34 self.base_timestamp + elapsed_secs
35 }
36
37 pub fn detect_clock_jump(&self, new_ntp_timestamp: i64, threshold: i64) -> bool {
39 let current_estimate = self.get_current_timestamp();
40 let drift = (new_ntp_timestamp - current_estimate).abs();
41
42 if drift > threshold {
43 warn!(
44 drift_secs = drift,
45 threshold_secs = threshold,
46 "检测到系统时钟跳跃"
47 );
48 true
49 } else {
50 debug!(drift_secs = drift, "系统时钟漂移在正常范围内");
51 false
52 }
53 }
54
55 pub fn update_timestamp(&mut self, new_timestamp: i64, is_jump: bool) {
57 self.base_timestamp = new_timestamp;
58 self.base_instant = Instant::now();
59 self.last_sync_at = self.base_instant;
60
61 if is_jump {
62 warn!("检测到系统时钟跳跃,重新初始化计时器");
63 } else {
64 debug!("基准时间戳已更新(漂移纠正)");
65 }
66 }
67
68 pub fn should_sync(&self, sync_interval: u64) -> bool {
70 self.last_sync_at.elapsed().as_secs() >= sync_interval
71 }
72
73 pub fn is_initialized(&self) -> bool {
75 self.initialized
76 }
77
78 pub fn set_initialized(&mut self, initialized: bool) {
80 self.initialized = initialized;
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87 use std::thread;
88
89 #[test]
90 fn test_timer_creation() {
91 let timer = GlobalTimer::new(1000);
92 assert_eq!(timer.base_timestamp, 1000);
93 }
94
95 #[test]
96 fn test_get_current_timestamp() {
97 let timer = GlobalTimer::new(1000);
98 thread::sleep(std::time::Duration::from_millis(100));
99 let ts = timer.get_current_timestamp();
100 assert!(ts >= 1000);
101 }
102
103 #[test]
104 fn test_detect_clock_jump() {
105 let timer = GlobalTimer::new(1000);
106 assert!(!timer.detect_clock_jump(1001, 5));
107 assert!(timer.detect_clock_jump(1010, 5));
108 }
109
110 #[test]
111 fn test_update_timestamp() {
112 let mut timer = GlobalTimer::new(1000);
113 timer.update_timestamp(2000, true);
114 assert_eq!(timer.base_timestamp, 2000);
115 }
116
117 #[test]
118 fn test_should_sync() {
119 let timer = GlobalTimer::new(1000);
120 assert!(!timer.should_sync(10));
121 thread::sleep(std::time::Duration::from_secs(1));
122 assert!(timer.should_sync(0));
123 }
124
125 #[test]
126 fn test_is_initialized() {
127 let timer = GlobalTimer::new(1000);
128 assert!(timer.is_initialized());
129 }
130
131 #[test]
132 fn test_set_initialized() {
133 let mut timer = GlobalTimer::new(1000);
134 timer.set_initialized(false);
135 assert!(!timer.is_initialized());
136 timer.set_initialized(true);
137 assert!(timer.is_initialized());
138 }
139}