async-throttle

Crates.ioasync-throttle
lib.rsasync-throttle
version0.3.2
sourcesrc
created_at2020-06-02 13:50:17.603375
updated_at2023-03-31 04:30:56.853418
descriptionAsynchronous Rate Limiting
homepagehttps://github.com/wcygan/async-throttle
repositoryhttps://github.com/wcygan/async-throttle
max_upload_size
id249254
size17,976
Will Cygan (wcygan)

documentation

README

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;
}
Commit count: 5

cargo fmt