leaky-bucket-lite

Crates.ioleaky-bucket-lite
lib.rsleaky-bucket-lite
version0.5.2
sourcesrc
created_at2021-01-14 17:12:46.082173
updated_at2022-02-17 19:20:24.281222
descriptionSlimmed down, lazy futures-aware rate limiter implementation.
homepagehttps://github.com/Gelbpunkt/leaky-bucket-lite
repositoryhttps://github.com/Gelbpunkt/leaky-bucket-lite
max_upload_size
id342002
size65,212
Jens Reidel (Gelbpunkt)

documentation

https://docs.rs/leaky-bucket-lite

README

leaky-bucket-lite

docs badge crates badge actions badge

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:

leaky-bucket-lite = "0.4"

Features

leaky-bucket-lite provides 3 implementations:

  • [LeakyBucket] (thread-safe, available via the tokio feature, which is on by default)
  • [sync_threadsafe::LeakyBucket] (thread-safe, available via the sync-threadsafe feature)
  • [sync::LeakyBucket] (not thread-safe, available via the sync feature).

For potential performance increase with sync-threadsafe or tokio using [parking_lot]'s locking objects, enable the parking_lot feature.

Example

use leaky_bucket_lite::LeakyBucket;
use std::time::Duration;

#[tokio::main]
async fn main() {
    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!");
}
Commit count: 83

cargo fmt