ratelim

Crates.ioratelim
lib.rsratelim
version0.2.2
created_at2024-07-25 22:22:14.707746+00
updated_at2025-06-30 19:32:18.002123+00
descriptionSimplistic rate limiter
homepage
repositoryhttps://github.com/vvv/rate-limiter
max_upload_size
id1315512
size7,356
Valeriy V. Vorotyntsev (vvv)

documentation

README

Simplistic rate limiter

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);
}
Commit count: 5

cargo fmt