A token-based rate limiter based on the [leaky bucket] algorithm.
If the tokens are already available, the acquisition will be instant through a fast path, and the acquired number of tokens will be added to the bucket.
If the bucket overflows (i.e. goes over max), the task that tried to acquire the tokens will be suspended until the required number of tokens has been added.
Add the following to your Cargo.toml
:
toml
leaky-bucket = "0.8.2"
If the project is built with the static
feature (default), you can use
[LeakyBucket] directly as long as you are inside a Tokio runtime, like so:
```rust use leaky_bucket::LeakyBucket; use std::{error::Error, time::Duration};
async fn main() -> Result<(), Box
println!("Waiting for permit...");
// should take about 5 seconds to acquire.
rate_limiter.acquire(10).await?;
println!("I made it!");
Ok(())
} ```
Note that only one coordinator instance will be lazily created, and it's associated with the runtime from within it was constructed. To use multiple runtimes you must use an explicit coordinator as documented in the next section.
Leaky buckets require coordination. By default, this will happen through a static coordinator spawned through [tokio::spawn] at first use. If you want to spawn the coordinator yourself, you can do the following with [LeakyBuckets]:
```rust use leaky_bucket::LeakyBuckets; use std::{error::Error, time::Duration};
async fn main() -> Result<(), Box
// spawn the coordinate thread to refill the rate limiter.
tokio::spawn(async move { coordinator.await.expect("coordinator errored") });
let rate_limiter = buckets
.rate_limiter()
.max(5)
.tokens(5)
.build()?;
println!("Waiting for permit...");
// should take about 5 seconds to acquire.
rate_limiter.acquire(10).await?;
println!("I made it!");
Ok(())
} ```
Note that since this coordinator uses timing facilities from tokio it has to
be spawned within a Tokio runtime with the [time
feature] enabled.
License: MIT/Apache-2.0