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.
Add the following to your Cargo.toml
:
toml
leaky-bucket-lite = "0.3"
leaky-bucket-lite provides 3 implementations:
leaky_bucket_lite::LeakyBucket
(thread-safe, available via the tokio
feature, which is on by default)leaky_bucket_lite::sync_threadsafe::LeakyBucket
(thread-safe, available via the sync-threadsafe
featureleaky_bucket_lite::sync::LeakyBucket
(not thread-safe, available via the sync
feature).For potential performance increase with sync-threadsafe
using parking_lot
's locking objects, enable the parking-lot
feature.
```rust use leakybucketlite::LeakyBucket; use std::{error::Error, time::Duration};
async fn main() -> Result<(), Box
License: MIT/Apache-2.0