3 releases

0.3.2 Mar 31, 2023
0.3.1 Mar 30, 2023
0.3.0 Mar 28, 2023
0.2.0 Aug 30, 2020
0.1.0 Jun 2, 2020

#2 in #throttle

Download history 2972/week @ 2024-11-30 2730/week @ 2024-12-07 2539/week @ 2024-12-14 921/week @ 2024-12-21 1969/week @ 2024-12-28 2448/week @ 2025-01-04 3597/week @ 2025-01-11 3617/week @ 2025-01-18 3965/week @ 2025-01-25 2950/week @ 2025-02-01 4470/week @ 2025-02-08 3022/week @ 2025-02-15 4149/week @ 2025-02-22 3375/week @ 2025-03-01 3978/week @ 2025-03-08 2650/week @ 2025-03-15

14,711 downloads per month
Used in 2 crates (via tokio-utils)

MIT license

18KB
277 lines

Async Throttle

github crates.io docs.rs build status codecov

Asynchronous Rate Limiting

This crate provides coarse and fine-grained ways to throttle the rate at which asynchronous tasks are executed.

Usage

Add this to your Cargo.toml:

[dependencies]
async-throttle = "0.3.2"

You can use the fine-grained rate limiter like so:

#[tokio::main]
async fn main() {
   let period = std::time::Duration::from_secs(5);
   let rate_limiter = MultiRateLimiter::new(period);
    
   // This completes instantly
   rate_limiter.throttle("foo", || computation()).await;

   // This completes instantly
   rate_limiter.throttle("bar", || computation()).await;

   // This takes 5 seconds to complete because the key "foo" is rate limited
   rate_limiter.throttle("foo", || computation()).await;
}

Dependencies

~3.5–9.5MB
~78K SLoC