skp-ratelimit
Advanced, modular rate limiting library for Rust with multiple algorithms, per-route quotas, and framework middleware.
Features
- 7 Algorithms: GCRA, Token Bucket, Leaky Bucket, Sliding Log, Sliding Window, Fixed Window, Concurrent
- 2 Storage Backends: Memory with GC, Redis with connection pooling
- 2 Framework Middleware: Axum (Tower), Actix-web
- Key Extractors: IP, Path, Header, Composite keys
- Per-Route Quotas: Different limits for different endpoints
- Policy System: Penalty on errors, credit for cached responses
Algorithm Comparison
| Algorithm | Best For | Memory | Burst Handling |
|---|---|---|---|
| GCRA | Precise rate control | Low | Excellent |
| Token Bucket | Bursty traffic | Low | Excellent |
| Leaky Bucket | Smooth output | Low | None |
| Sliding Log | Precision critical | High | Good |
| Sliding Window | General purpose | Low | Good |
| Fixed Window | Simple use cases | Low | Poor |
Quick Start
use *;
async
Quota vs Burst
A Quota consists of two key parameters:
| Parameter | Description |
|---|---|
max_requests |
Sustained rate - requests allowed per time window |
burst |
Instant capacity - requests allowed in quick succession |
Burst Configuration Options
// Default: burst = max_requests (10 burst, 10/sec rate)
per_second
// Strict burst: limits instant requests while allowing higher sustained rate
per_second.with_burst // Only 5 instant, 10/sec rate
// Generous burst: allows traffic spikes, borrows from future quota
per_second.with_burst // 50 instant, 10/sec rate
When to Use Each
| Scenario | Configuration | Reason |
|---|---|---|
| API protection | burst < max_requests |
Prevent thundering herd |
| User-facing apps | burst > max_requests |
Better UX, forgiving spikes |
| General use | burst = max_requests |
Balanced (default) |
Example Flow
Quota::per_second(10).with_burst(3)
Time 0s: [###.......] 3 tokens available (burst limit)
Request 1: [##........] ✅ Allowed, 2 remaining
Request 2: [#.........] ✅ Allowed, 1 remaining
Request 3: [..........] ✅ Allowed, 0 remaining
Request 4: [..........] ❌ DENIED (wait for refill)
~100ms later: 1 token refills (10/sec = 1 per 100ms)
Request 5: [..........] ✅ Allowed
Per-Route Rate Limiting
use ;
let manager = builder
.default_quota
.route
.route
.route_pattern
.build_with_key;
let decision = manager.check_and_record.await?;
Axum Middleware
use ;
use ;
let app = new
.route
.layer;
Actix-web Middleware
use ;
use ;
new
Redis Storage
use ;
let config = new
.with_prefix
.with_pool_size;
let storage = new.await?;
Composite Keys
Rate limit by multiple factors (IP + Path, User + API Key):
use CompositeKey;
let key = new;
// Generates keys like "192.168.1.1:/api/users"
Policy System
use ;
// Consume extra tokens on errors, refund on 304 Not Modified
let policy = new
.with // 2x cost on 4xx/5xx
.with; // Refund on 304
Feature Flags
| Feature | Description | Default |
|---|---|---|
memory |
In-memory storage with GC | ✓ |
redis |
Redis storage with pooling | |
axum |
Axum middleware | |
actix |
Actix-web middleware | |
gcra |
GCRA algorithm | ✓ |
leaky-bucket |
Leaky bucket algorithm | ✓ |
sliding-log |
Sliding log algorithm | ✓ |
concurrent |
Concurrent limiter | ✓ |
full |
All features |
Examples
Benchmarks
License
MIT