52 lines
1.3 KiB
Rust
52 lines
1.3 KiB
Rust
use core::fmt;
|
|
use log::*;
|
|
use std::time::Duration;
|
|
use std::time::Instant;
|
|
|
|
pub struct ThrottleTrace {
|
|
ivl: Duration,
|
|
next: Instant,
|
|
count: u64,
|
|
}
|
|
|
|
impl ThrottleTrace {
|
|
pub fn new(ivl: Duration) -> Self {
|
|
Self {
|
|
ivl,
|
|
next: Instant::now(),
|
|
count: 0,
|
|
}
|
|
}
|
|
|
|
pub fn trigger(&mut self, msg: &str, params: &[&dyn fmt::Debug]) {
|
|
self.count += 1;
|
|
let tsnow = Instant::now();
|
|
if self.next <= tsnow {
|
|
self.next = tsnow + self.ivl;
|
|
if params.len() == 1 {
|
|
debug!("{} {:?} (count {})", msg, params[0], self.count);
|
|
} else if params.len() == 2 {
|
|
debug!("{} {:?} {:?} (count {})", msg, params[0], params[1], self.count);
|
|
} else if params.len() == 3 {
|
|
debug!(
|
|
"{} {:?} {:?} {:?} (count {})",
|
|
msg, params[0], params[1], params[2], self.count
|
|
);
|
|
} else {
|
|
debug!("{} (count {})", msg, self.count);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn is_action(&mut self) -> bool {
|
|
self.count += 1;
|
|
let tsnow = Instant::now();
|
|
if self.next <= tsnow {
|
|
self.next = tsnow + self.ivl;
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|