tokio_rate_limit/lib.rs
1//! # tokio-rate-limit
2//!
3//! High-performance rate limiting library with lock-free token accounting and sharded state management.
4//!
5//! ## Features
6//!
7//! - **Lock-free token accounting**: Atomic operations on token counts (10M+ ops/sec)
8//! - **Sharded state management**: Fine-grained per-shard locking via DashMap
9//! - **Pluggable algorithms**: Token bucket (more coming soon)
10//! - **Axum middleware**: Drop-in rate limiting for web apps
11//! - **Zero allocations**: In the hot path
12//!
13//! ## Architecture
14//!
15//! This library uses a hybrid approach for maximum performance:
16//! - **Token updates**: True lock-free atomic compare-and-swap operations
17//! - **Key lookup**: Sharded concurrent hashmap (DashMap) with per-shard locking
18//!
19//! This design provides excellent concurrency while maintaining per-key isolation.
20//!
21//! ## Quick Start
22//!
23//! ```rust,no_run
24//! use tokio_rate_limit::{RateLimiter, RateLimiterConfig};
25//!
26//! #[tokio::main]
27//! async fn main() {
28//! let limiter = RateLimiter::new(RateLimiterConfig {
29//! requests_per_second: 100,
30//! burst: 200,
31//! });
32//!
33//! let decision = limiter.check("client-id").await.unwrap();
34//! if decision.permitted {
35//! // Process request
36//! } else {
37//! // Rate limit exceeded, retry after decision.retry_after
38//! }
39//! }
40//! ```
41
42#![warn(missing_docs, rust_2024_compatibility)]
43#![deny(unsafe_code)]
44
45pub mod algorithm;
46mod error;
47mod limiter;
48
49#[cfg(feature = "middleware")]
50pub mod middleware;
51
52#[cfg(feature = "tonic-support")]
53pub mod tonic_middleware;
54
55pub use error::{Error, Result};
56pub use limiter::{RateLimitDecision, RateLimiter, RateLimiterBuilder, RateLimiterConfig};
57
58/// Re-export of the rate limiter algorithm trait for custom implementations
59pub use algorithm::Algorithm;