#节流 #定时器 #事件 #统计 #记录

throttle-timer

使用简单库节流事件并记录事件统计

6 个版本 (1 个稳定版)

1.0.0 2019 年 11 月 8 日
0.0.6 2019 年 11 月 7 日

#14 in #节流

Apache-2.0 OR MIT

12KB
183 行代码(不含注释)

Rust throttle_timer

简单的 Rust 库,用于节流事件并记录事件统计

Docs Build Status Coverage Status Current Crates.io Version License

安装 Cargo.toml

throttle-timer = "1.0.0"

示例用法

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

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

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

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

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

无运行时依赖