rate-guard-core
A comprehensive rate limiting library for Rust applications with multiple thread-safe algorithms.
Features
- 5 Rate Limiting Algorithms: Token Bucket, Leaky Bucket, Fixed Window, Sliding Window, and Approximate Sliding Window
- Thread-Safe: All algorithms use non-blocking locks
- Zero Dependencies: Lightweight with no external dependencies
- Flexible Time: Works with any time unit via abstract “ticks”
- Configurable Tick Precision: Compile-time feature flags allow choosing
u64(default) oru128for tick units - Rust 1.60+: Compatible with older Rust versions
Quick Start
from crate.io
Add to your Cargo.toml:
[]
= { = "0.2.2" }
Tick Precision (u64 / u128)
By default, the crate uses u64 as the tick unit, allowing up to ~584 years of nanosecond-resolution time.
If your application needs ultra-long durations or ultra-high precision, you can enable u128 support via feature flags:
[]
= { = "0.2.2", = false, = ["tick_u128"] }
from Github
Add to your Cargo.toml:
[]
= { = "https://github.com/Kuanlin/rate-guard-core", = "v0.2.2" }
Tick Precision (u64 / u128)
By default, the crate uses u64 as the tick unit, allowing up to ~584 years of nanosecond-resolution time.
If your application needs ultra-long durations or ultra-high precision, you can enable u128 support via feature flags:
[]
= { = "https://github.com/Kuanlin/rate-guard-core", = "v0.2.2", = false, = ["tick_u128"] }
Usage Examples
Token Bucket
Perfect for APIs that allow occasional bursts while maintaining average rate:
use TokenBucketCore;
// Allow 100 requests, refill 10 every 5 seconds
let limiter = new;
let current_tick = now
.duration_since
.unwrap
.as_secs;
match limiter.try_acquire_at
Leaky Bucket
Great for maintaining steady traffic flow:
use LeakyBucketCore;
// capacity = 50 tokens, refill = 5 tokens per 10 ticks
let limiter = new;
assert_eq!;
Fixed Window Counter
use FixedWindowCounterCore;
// capacity = 100 per fixed window, window size = 60 ticks
let limiter = new;
assert_eq!;
Sliding Window Counter
use SlidingWindowCounterCore;
// Window of 100 tokens across 6 buckets of 10 ticks each (50 ticks window)
let limiter = new;
assert_eq!;
Approximate Sliding Window
(A Memory-Optimized Version: Sliding Window Counter)
formula: UsedCapacities = (1-X%) * lastWindowRequests + currentWindowRequests. (X is the propotion of request time within the current window.)
use ApproximateSlidingWindowCore;
// Create ApproximateSlidingWindow limiter with capacity 100, window size 60 ticks
let limiter = new;
assert_eq!;
Error Handling
All rate limiters return an AcquireResult:
use ;
match limiter.try_acquire_at
Time Management
The library uses abstract “ticks” for time. You can map any time source:
// Seconds
let tick = now.duration_since.unwrap.as_secs;
// Milliseconds
let tick = now.duration_since.unwrap.as_millis as u64;
// Custom time
let tick = my_monotonic_timer.elapsed_ticks;
Thread Safety
use Arc;
use thread;
use TokenBucketCore;
let limiter = new;
for _ in 0..10
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.