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

#938 in Asynchronous

Download history 84/week @ 2024-01-05 59/week @ 2024-01-12 85/week @ 2024-01-19 350/week @ 2024-01-26 327/week @ 2024-02-02 427/week @ 2024-02-09 496/week @ 2024-02-16 291/week @ 2024-02-23 643/week @ 2024-03-01 889/week @ 2024-03-08 1389/week @ 2024-03-15 2135/week @ 2024-03-22 2692/week @ 2024-03-29 2272/week @ 2024-04-05 3054/week @ 2024-04-12 1826/week @ 2024-04-19

10,300 downloads per month
Used in 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–10MB
~75K SLoC