#rate-limiting #async-context #tokio #rate #async #limiting #sliding-window

async-rate-limit

Common traits for rate limiting and implementations in async contexts

4 releases

new 0.1.1 Nov 3, 2024
0.1.0 Jun 19, 2024
0.0.4 Mar 8, 2024
0.0.3 Oct 4, 2023

#630 in Asynchronous

Download history 53/week @ 2024-07-07 38/week @ 2024-07-14 27/week @ 2024-07-21 64/week @ 2024-07-28 1/week @ 2024-08-04 6/week @ 2024-08-11 58/week @ 2024-08-18 13/week @ 2024-08-25 108/week @ 2024-09-01 45/week @ 2024-09-08 13/week @ 2024-09-15 21/week @ 2024-09-22 7/week @ 2024-09-29 38/week @ 2024-10-06 36/week @ 2024-10-13 12/week @ 2024-10-20

94 downloads per month
Used in kraken-async-rs

MIT license

27KB
374 lines

async-rate-limit

A basic Tokio rate-limiting library providing two traits and common implementations.

badge License: MIT

Usage:

use tokio::time::{Instant, Duration};
use async_rate_limit::limiters::VariableCostRateLimiter;
use async_rate_limit::sliding_window::SlidingWindowRateLimiter;

#[tokio::main]
async fn main() -> () {
    let mut limiter = SlidingWindowRateLimiter::new(Duration::from_secs(1), 5);
    
    for _ in 0..3 {
        // these will proceed immediately, spending 3 units
        get_lite(&mut limiter).await;
    }
    // 3/5 units are spent, so this will wait for ~1s to proceed since it costs another 3
    get_heavy(&mut limiter).await;
}

// note the use of the `VariableCostRateLimiter` trait, rather than the direct type
async fn get_lite<T>(limiter: &mut T) where T: VariableCostRateLimiter {
    limiter.wait_with_cost(1).await;
    println!("Lite: {:?}", Instant::now());
}

async fn get_heavy<T>(limiter: &mut T) where T: VariableCostRateLimiter {
    limiter.wait_with_cost(3).await;
    println!("Heavy: {:?}", Instant::now());
}

Dependencies

~2.4–8.5MB
~58K SLoC