[][src]Crate throttle_timer

Throttle events and record event stats with a simple library

throttle_timer has no dependencies

ThrottleTimer struct is created with a max frequency and label

ThrottleTimer::new(Duration::from_secs(1_u64), &"Once every second");

Calling run() will check the last call time. If max frequency time has not passed the fn will return false. If max_frequency duration has passed since the last call then the fn will return true

Example

use std::time::Duration;
use throttle_timer::ThrottleTimer;

let mut break_timer = ThrottleTimer::new(Duration::from_secs(10_u64), &"Break");
let mut val = 0_u8;

// timers always run when no previous runs
break_timer.run(&mut || val += 1);
for _ in 0..100 {
    // timer will not run as 10 secs has not passed
    // do run will return false
    break_timer.run(&mut || val += 1);
}

break_timer.print_stats();
// Break called 0/sec, total calls 1, has been running for 10us

assert_eq!(break_timer.total_calls(), &1);
assert_eq!(val, 1_u8);

Structs

ThrottleTimer