[][src]Crate leaky_bucket_lite

Documentation Crates Actions Status

A token-based rate limiter based on the leaky bucket algorithm.

The implementation is fair: Whoever acquires first will be served first.

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 are not available, it will wait until enough tokens are available.

Usage

Add the following to your Cargo.toml:

leaky-bucket-lite = "0.1.0"

Example

use leaky_bucket_lite::LeakyBucket;
use std::{error::Error, time::Duration};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let rate_limiter = LeakyBucket::builder()
        .max(5)
        .tokens(0)
        .refill_interval(Duration::from_secs(1))
        .refill_amount(1)
        .build();

    println!("Waiting for permit...");
    // should take about 5 seconds to acquire.
    rate_limiter.acquire(5).await?;
    println!("I made it!");
    Ok(())
}

Structs

Builder

Builder for a leaky bucket.

LeakyBucket

The leaky bucket.

Type Definitions

Error

Error type used in this crate.