5 releases

new 0.2.2 Jun 30, 2025
0.2.1 Feb 27, 2025
0.2.0 Feb 27, 2025
0.1.1 Jul 25, 2024
0.1.0 Jul 25, 2024

#246 in Date and time

Download history 31/week @ 2025-03-10 58/week @ 2025-03-17 48/week @ 2025-03-24 66/week @ 2025-03-31 30/week @ 2025-04-07 40/week @ 2025-04-14 78/week @ 2025-04-21 51/week @ 2025-04-28 14/week @ 2025-05-05 120/week @ 2025-05-12 229/week @ 2025-05-19 65/week @ 2025-05-26 39/week @ 2025-06-02 90/week @ 2025-06-09 71/week @ 2025-06-16 49/week @ 2025-06-23

249 downloads per month

MIT license

6KB
58 lines

Simplistic rate limiter.

Examples

#
// We don't want to overwater plants. Twice a second should be fine?
let mut lim_water_plants = RateLimiter::new(Duration::from_millis(500));

let mut n = 0;
for _ in 0..5 {
    lim_water_plants.run(|| {
        println!("Watering plants... 🌱🔫");
        n += 1;
    });
    thread::sleep(Duration::from_millis(200));
}
assert_eq!(n, 2);

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);
}

No runtime deps