1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! Reusable rate limiting library
//!
//! Provides a flexible, high-performance rate limiting solution with:
//! - Lock-free concurrent access using DashMap
//! - Configurable per-minute and per-hour limits
//! - Plan-based configuration (free, pro, enterprise)
//! - Optional Axum middleware integration
//!
//! # Basic Usage
//!
//! ```rust
//! use gatekpr_rate_limiter::{RateLimitConfig, RateLimitStore};
//!
//! // Create a store to track rate limits
//! let store = RateLimitStore::new();
//!
//! // Get config for a plan
//! let config = RateLimitConfig::for_plan("pro");
//!
//! // Check rate limit for a key (e.g., user ID, IP, tenant)
//! let result = store.check("user-123", &config);
//!
//! if result.is_allowed() {
//! // Process the request
//! } else {
//! // Return 429 Too Many Requests
//! if let Some(retry_after) = result.retry_after() {
//! println!("Rate limited. Retry after {} seconds", retry_after);
//! }
//! }
//! ```
//!
//! # Custom Configuration
//!
//! ```rust
//! use gatekpr_rate_limiter::RateLimitConfigBuilder;
//!
//! let config = RateLimitConfigBuilder::new()
//! .per_minute(200)
//! .per_hour(10000)
//! .build();
//! ```
//!
//! # Axum Integration (requires `axum` feature)
//!
//! ```rust,ignore
//! use gatekpr_rate_limiter::axum::RateLimitLayer;
//! use axum::Router;
//!
//! let app = Router::new()
//! .route("/api", get(handler))
//! .layer(RateLimitLayer::new());
//! ```
// Re-exports
pub use ;
pub use ;
pub use ;
/// Prelude module for convenient imports