Expand description
A minimal, auditable, lock-free token bucket rate limiter.
§Design goals
- Zero dependencies
- Zero heap allocations
- No
unsafe no_stdcompatible core- Deterministic tests via custom
Clockimplementations
§Behavior guarantees
RateLimiter::remainingis always bounded bycapacityRateLimiter::allow_nis atomic for token deduction- If time goes backwards (
now < last_refill), refill is skipped - All public operations are panic-free
§Example (std)
use ratelock::RateLimiter;
let limiter = RateLimiter::new(10, 5);
assert!(limiter.allow());
assert_eq!(limiter.remaining(), 9);§Example (no_std compatible API)
use ratelock::{Clock, RateLimiter};
struct FixedClock(u64);
impl Clock for FixedClock {
fn now_ns(&self) -> u64 {
self.0
}
}
let clock = FixedClock(0);
let limiter = RateLimiter::with_clock(3, 0, clock);
assert!(limiter.allow());
assert!(limiter.allow());
assert!(limiter.allow());
assert!(!limiter.allow());Structs§
- Rate
Limiter - Non-allocating lock-free token bucket.
- Sharded
Rate Limiter - A fixed-size shard set of independent
RateLimiterinstances. - Snapshot
- Best-effort state snapshot for observability.
- StdClock
stdclock implementation backed bystd::time::Instant.
Traits§
- Clock
- Monotonic time source used by
crate::RateLimiter.