Expand description
§Flux Limiter
A high-performance rate limiter based on the Generic Cell Rate Algorithm (GCRA).
§Quick Example
use flux_limiter::{FluxLimiter, FluxLimiterConfig, SystemClock};
let config = FluxLimiterConfig::new(10.0, 5.0);
let limiter = FluxLimiter::with_config(config, SystemClock).unwrap();
let decision = limiter.check_request("user_123").unwrap();
if decision.allowed {
println!("Request allowed");
} else {
println!("Rate limited - retry after {:.2}s",
decision.retry_after_seconds.unwrap_or(0.0));
}Structs§
- Flux
Limiter - The main FluxLimiter model.
T is the type used to identify clients (e.g., String, u64, etc.).
C is the clock type, defaulting to SystemClock.
We use
Arc<DashMap>for thread-safe concurrent access to client state. - Flux
Limiter Config - Configuration for rate limiter behavior.
- Flux
Limiter Decision - Result of a rate limiting decision with metadata for HTTP responses
- System
Clock - SystemClock implementation using the system time. Returns the current time in nanoseconds since the Unix epoch. Panics if the system clock is before the Unix epoch. This is the default clock used in the RateLimiter. Implements the Clock trait. Thread-safe and can be shared across threads.
Enums§
- Clock
Error - Clock error type
- Flux
Limiter Error - Error type for FluxLimiter configuration issues.
Traits§
- Clock
- Clock trait to abstract time retrieval.
Implementors must be thread-safe (Send + Sync).
The
nowmethod returns the current time in nanoseconds as a u64. This trait allows for different clock implementations, such as system time or a test clock. The Clock trait is used by the RateLimiter to get the current time.