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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;
pub struct ThrottleTimer {
maybe_last_called_time: Option<Instant>,
total_calls: usize,
created_date: SystemTime,
max_frequency: Duration,
event_name: &'static str,
}
impl ThrottleTimer {
pub fn new(max_frequency: std::time::Duration, event_name: &'static str) -> Self {
Self {
maybe_last_called_time: None,
max_frequency,
event_name,
total_calls: 0,
created_date: SystemTime::now(),
}
}
pub const fn total_calls(&self) -> &usize {
&self.total_calls
}
pub const fn max_frequency(&self) -> &Duration {
&self.max_frequency
}
pub const fn created_date(&self) -> SystemTime {
self.created_date
}
pub fn print_stats(&self) {
match self.created_date.elapsed() {
Ok(created_time_elapsed) => {
println!(
"{} calls {}/sec, total calls {}, has been running for {:?}",
self.event_name,
created_time_elapsed.as_secs() / self.total_calls as u64,
self.total_calls,
created_time_elapsed,
);
}
Err(e) => eprintln!("{:?}", e),
}
}
pub fn do_run(&mut self) -> bool {
let now = Instant::now();
let do_run_flag: bool = match self.maybe_last_called_time {
None => true,
Some(last_time) => now.duration_since(last_time) >= self.max_frequency,
};
if do_run_flag {
self.maybe_last_called_time = Some(now);
self.total_calls += 1;
}
do_run_flag
}
pub fn do_run_with_msg(&mut self) -> bool {
let do_run_flag: bool = self.do_run();
if !do_run_flag {
println!(
"{} throttled, last time {:?}",
self.event_name,
Instant::now().duration_since(self.maybe_last_called_time.unwrap())
);
}
do_run_flag
}
}
#[cfg(test)]
mod test {
use super::ThrottleTimer;
use std::{thread, time::Duration};
#[test]
fn test_do_run() {
let mut break_timer: ThrottleTimer =
ThrottleTimer::new(Duration::from_secs(45_000_u64), &"Break");
let do_run_flag = break_timer.do_run();
assert!(do_run_flag);
if do_run_flag {
println!("timer do run flag is set to true")
}
}
#[test]
fn test_do_run_with_msg() {
let mut break_timer: ThrottleTimer =
ThrottleTimer::new(Duration::from_secs(45_000_u64), &"Break");
let do_run_flag = break_timer.do_run_with_msg();
assert!(do_run_flag);
if do_run_flag {
println!("timer do run flag is set to true")
}
break_timer.total_calls();
break_timer.max_frequency();
break_timer.created_date();
}
#[test]
fn test_call_count() {
let mut break_timer: ThrottleTimer =
ThrottleTimer::new(Duration::from_nanos(1_u64), &"Break");
for _ in 0..100 {
assert_eq!(break_timer.do_run(), true);
thread::sleep(Duration::from_nanos(100_u64));
}
assert_eq!(break_timer.total_calls, 100);
break_timer.print_stats();
}
#[test]
fn test_in_loop() {
let mut break_timer = ThrottleTimer::new(Duration::from_secs(10_u64), &"Break");
assert!(break_timer.do_run());
for _ in 0..100 {
assert!(!break_timer.do_run());
}
assert_eq!(break_timer.total_calls(), &1);
}
#[test]
fn test_with_delay() {
let mut snack_timer: ThrottleTimer =
ThrottleTimer::new(Duration::from_secs(1_u64), &"Snack");
let do_run_flag = snack_timer.do_run();
assert!(do_run_flag);
if do_run_flag {
println!("timer do run flag is set to true")
}
let do_run_flag2 = snack_timer.do_run_with_msg();
assert_eq!(do_run_flag2, false);
if !do_run_flag2 {
println!("timer flag returned execute is set to false. Execution too soon")
}
thread::sleep(snack_timer.max_frequency);
assert!(snack_timer.do_run());
thread::sleep(Duration::from_millis(100_u64));
assert!(!snack_timer.do_run());
thread::sleep(Duration::from_secs(1_u64));
assert!(snack_timer.do_run());
}
}