Skip to content

vvv/rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simplistic rate limiter

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages