Crates.io | ratelim |
lib.rs | ratelim |
version | 0.1.1 |
source | src |
created_at | 2024-07-25 22:22:14.707746 |
updated_at | 2024-07-25 23:05:41.858706 |
description | Simplistic rate limiter |
homepage | |
repository | https://github.com/vvv/rate-limiter |
max_upload_size | |
id | 1315512 |
size | 5,839 |
Useful in cases when you don't want to repeat an operation too often. For example, to prevent flooding the logs with the same message.
Example:
use std::{thread, time::Duration};
use ratelim::RateLimiter;
use tracing::warn;
fn main() {
let mut lim_warn_oddities = RateLimiter::new(Duration::from_millis(10));
let mut n = 0;
for i in 0..1000 {
lim_warn_oddities.run(|| {
if i % 2 != 0 {
warn!("{} is odd. Oh my!", i);
n += 1;
}
});
thread::sleep(Duration::from_micros(100));
}
assert!(0 < n && n < 10);
}