A powerful, lock-free rate limiter for Rust: multiple algorithms behind one trait, sharded per-key state, bounded-memory eviction, retry-after, and a one-line Tier-1 API. Built against hostile traffic.
//! Deterministic testing: drive refills with a `ManualClock`, no real sleeping.
//!//! ```text
//! cargo run --example mock_clock
//! ```
useclock_lib::ManualClock;userate_net::RateLimiter;usestd::sync::Arc;usestd::time::Duration;fnmain(){// Share a manual clock with the limiter so the test drives time by hand.
let clock =Arc::new(ManualClock::new());let limiter =RateLimiter::per_second(3).with_clock(Arc::clone(&clock));for i in1..=3{assert!(limiter.check("k").is_allow());println!("request {i}: allowed");}assert!(limiter.check("k").is_deny());println!("request 4: denied (allowance spent)");// Advance one second — instantly, with no thread::sleep.
clock.advance(Duration::from_secs(1));println!("\n-- advanced clock by 1s (no real time elapsed) --\n");assert!(limiter.check("k").is_allow());println!("request 5: allowed (allowance refilled)");}