| Crates.io | shot-limit |
| lib.rs | shot-limit |
| version | 0.1.1 |
| created_at | 2026-01-22 09:08:56.040812+00 |
| updated_at | 2026-01-22 16:46:15.396615+00 |
| description | multiple strategy rate limiting library |
| homepage | https://github.com/garypen/rate-limiting |
| repository | https://github.com/garypen/rate-limiting |
| max_upload_size | |
| id | 2061155 |
| size | 69,991 |
Core atomic rate-limiting strategies for high-throughput Rust services. This crate provides the lock-free logic used by the tower-shot middleware ecosystem.
std::sync::atomic and the quanta TSC-based clock for state management.Mutex or RwLock contention.Built for extreme scale on modern hardware. The following benchmarks were recorded on an Apple M1 (8-core) using the included criterion suite:
| Strategy | Single-Threaded | 8-Thread Parallel |
|---|---|---|
| Token Bucket | 3.11 ns | 0.71 ns |
| Fixed Window | 2.58 ns | 0.42 ns |
| Sliding Window | 4.94 ns | 1.09 ns |
| GCRA | 2.32 ns | 0.41 ns |
Note: Total throughput at 8 threads exceeds 2.3 billion operations per second for the Fixed Window strategy.
Each strategy implements the Strategy trait, which provides a process() method.
use shot_limit::TokenBucket;
use shot_limit::Strategy;
use std::time::Duration;
use std::num::NonZeroUsize;
let capacity = NonZeroUsize::new(100).unwrap();
let increment = NonZeroUsize::new(100).unwrap();
let period = Duration::from_secs(60);
// Initialize a Token Bucket with a capacity of 100 tokens, refilling 100 tokens every minute
let bucket = TokenBucket::new(capacity, increment, period);
if bucket.process().is_continue() {
// Request allowed
} else {
// Rate limit exceeded
}
The most flexible strategy. It allows for a burst of requests up to a defined capacity and replenishes tokens at a steady rate. Best for smoothing out traffic spikes and providing a consistent experience.
Divides time into fixed slots (e.g., 1-minute windows). Simple and extremely low overhead, but can allow twice the rate limit at window boundaries. Use this when performance is the absolute priority and slight boundary bursts are acceptable.
A weighted algorithm that accounts for the previous window's traffic to smooth out boundary bursts. Provides significantly more accuracy than Fixed Window with only a minor performance trade-off for the additional floating-point calculations.
A highly efficient and mathematically elegant algorithm that provides a strict, predictable rate limit without the burstiness of a token bucket. It's an excellent choice when you need to enforce a smooth, even flow of traffic.
Run the benchmark suite to verify performance on your specific architecture. On high-performance ARM or x86 chips, you should see linear scaling across multiple threads.
cargo bench
Licensed under either of Apache License, Version 2.0 or MIT license at your option.