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

#1881 in Asynchronous

Download history 2895/week @ 2024-06-17 1757/week @ 2024-06-24 2035/week @ 2024-07-01 2301/week @ 2024-07-08 2963/week @ 2024-07-15 2605/week @ 2024-07-22 2509/week @ 2024-07-29 2878/week @ 2024-08-05 3107/week @ 2024-08-12 2572/week @ 2024-08-19 2562/week @ 2024-08-26 1385/week @ 2024-09-02 3311/week @ 2024-09-09 1691/week @ 2024-09-16 3609/week @ 2024-09-23 2094/week @ 2024-09-30

10,735 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.5–10MB
~76K SLoC