| Crates.io | rate_limiters |
| lib.rs | rate_limiters |
| version | 0.1.8 |
| created_at | 2025-08-27 14:43:28.823658+00 |
| updated_at | 2025-09-06 10:01:52.982363+00 |
| description | Flexible and easy-to-use Rust library for implementing rate limits. |
| homepage | https://github.com/NikitaSmithTheOne/rate-limiters-rs |
| repository | https://github.com/NikitaSmithTheOne/rate-limiters-rs |
| max_upload_size | |
| id | 1812785 |
| size | 86,948 |
Rate Limiter Algorithms for RustA Rust library implementing popular rate limiting algorithms with support for multithreaded usage.
⭐ If you find this crate interesting or useful, please give it a star — it really helps!
README versions:
This library provides simple and efficient implementations of rate limiting algorithms for Rust applications. Useful for controlling request flow or limiting load on your services. Available implementations:
Funny and easy-to-understand analogies that explain rate limiting algorithms.
Leaky BucketToken BucketFixed Window CounterSliding Window LogSliding Window Countercargo add rate_limiters
All usage examples can be found in the examples directory.
Leaky Bucket exampleCode example:
use std::thread;
use std::time::{Duration, Instant};
use rate_limiters::leaky_bucket::LeakyBucket;
use rate_limiters::token_bucket::r#impl::RateLimiter;
fn main() {
let start = Instant::now();
let mut bucket = LeakyBucket::new(3, 1.0);
for i in 0..100 {
bucket.refresh();
let limit = bucket.get_limit();
let remaining = bucket.get_remaining();
let used = bucket.get_used();
let reset = bucket.get_reset();
let is_acquired = bucket.try_acquire(1);
let elapsed = start.elapsed().as_secs_f32();
println!(
"[{elapsed:5.2}s] Request #{:03} | {:<12} | Limit: {:2} | Remaining: {:2} | Used: {:2} | Reset: {}",
i + 1,
if is_acquired {
"Allowed"
} else {
"Rate limited"
},
limit,
remaining,
used,
reset
);
thread::sleep(Duration::from_millis(300));
}
}
Output:
[ 0.00s] Request #001 | Allowed | Limit: 3 | Remaining: 3 | Used: 0 | Reset: 1756307371
[ 0.30s] Request #002 | Allowed | Limit: 3 | Remaining: 2 | Used: 1 | Reset: 1756307372
[ 0.60s] Request #003 | Allowed | Limit: 3 | Remaining: 2 | Used: 1 | Reset: 1756307373
[ 0.90s] Request #004 | Rate limited | Limit: 3 | Remaining: 1 | Used: 2 | Reset: 1756307374
[ 1.20s] Request #005 | Allowed | Limit: 3 | Remaining: 1 | Used: 2 | Reset: 1756307374
[ 1.50s] Request #006 | Rate limited | Limit: 3 | Remaining: 1 | Used: 2 | Reset: 1756307375
[ 1.81s] Request #007 | Rate limited | Limit: 3 | Remaining: 1 | Used: 2 | Reset: 1756307375
[ 2.11s] Request #008 | Allowed | Limit: 3 | Remaining: 1 | Used: 2 | Reset: 1756307375
[ 2.41s] Request #009 | Rate limited | Limit: 3 | Remaining: 0 | Used: 3 | Reset: 1756307376
[ 2.71s] Request #010 | Rate limited | Limit: 3 | Remaining: 1 | Used: 2 | Reset: 1756307376
MIT License. See LICENSE for details.