leaky-bucket

Documentation Crates Actions Status

A token-based rate limiter based on the [leaky bucket] algorithm, mainly a lazy reimplementation of [udoprog's leaky-bucket] with less dependencies and overhead.

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 they aren't, the task that tried to acquire the tokens will be suspended until the required number of tokens has been added.

Usage

Add the following to your Cargo.toml:

toml leaky-bucket-lite = "0.1.0"

Example

```rust use leakybucketlite::LeakyBucket; use std::{error::Error, time::Duration};

[tokio::main]

async fn main() -> Result<(), Box> { let ratelimiter = LeakyBucket::builder() .max(5) .tokens(0) .refillinterval(Duration::fromsecs(1)) .refillamount(1) .build(); println!("Waiting for permit..."); // should take about 5 seconds to acquire. rate_limiter.acquire(5).await?; println!("I made it!"); Ok(()) } ```

License: MIT/Apache-2.0