A token-based rate limiter based on the [leaky bucket] algorithm.
This library requires the user to add the following dependencies to use:
toml
leaky-bucket = "0.5.0"
```rust use leaky_bucket::LeakyBucket; use std::{error::Error, time::Duration};
async fn main() -> Result<(), Box
println!("Waiting for permit...");
// should take about ten seconds to get a permit.
rate_limiter.acquire_one().await?;
println!("I made it!");
Ok(())
} ```
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
let rate_limiter = buckets
.rate_limiter()
.max(100)
.refill_interval(Duration::from_secs(10))
.refill_amount(100)
.build()?;
println!("Waiting for permit...");
// should take about ten seconds to get a permit.
rate_limiter.acquire_one().await?;
println!("I made it!");
Ok(())
} ```