| Crates.io | throttlecrab |
| lib.rs | throttlecrab |
| version | 0.4.31 |
| created_at | 2025-07-29 20:09:08.006773+00 |
| updated_at | 2025-09-14 18:31:49.245574+00 |
| description | A high-performance GCRA (Generic Cell Rate Algorithm) rate limiter library |
| homepage | https://github.com/lazureykis/throttlecrab |
| repository | https://github.com/lazureykis/throttlecrab |
| max_upload_size | |
| id | 1772756 |
| size | 139,851 |
A high-performance GCRA (Generic Cell Rate Algorithm) rate limiter library for Rust.
Add this to your Cargo.toml:
[dependencies]
throttlecrab = "0.4"
use std::time::SystemTime;
use throttlecrab::{RateLimiter, PeriodicStore};
fn main() {
// Create a rate limiter with an in-memory store
let mut limiter = RateLimiter::new(PeriodicStore::new());
// Check if a request is allowed
// Parameters: key, max_burst, count_per_period, period (seconds), quantity, timestamp
let (allowed, result) = limiter
.rate_limit("api_key_123", 10, 100, 60, 1, SystemTime::now())
.unwrap();
if allowed {
println!("Request allowed! Remaining: {}", result.remaining);
} else {
println!("Rate limit exceeded! Retry after: {:?}", result.retry_after);
}
}
The library provides several store implementations optimized for different use cases:
The Generic Cell Rate Algorithm (GCRA) is a rate limiting algorithm that provides:
GCRA works by tracking the "Theoretical Arrival Time" (TAT) of requests, ensuring consistent spacing between allowed requests while permitting controlled bursts.
Looking for a standalone rate limiting server? Check out throttlecrab-server which provides HTTP and gRPC interfaces.
MIT(./LICENSE)