| Crates.io | ratelim |
| lib.rs | ratelim |
| version | 0.2.2 |
| created_at | 2024-07-25 22:22:14.707746+00 |
| updated_at | 2025-06-30 19:32:18.002123+00 |
| description | Simplistic rate limiter |
| homepage | |
| repository | https://github.com/vvv/rate-limiter |
| max_upload_size | |
| id | 1315512 |
| size | 7,356 |
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);
}