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
76
77
78
79
80
81
82
83
84
//! # ntex-ratelimiter
//!
//! `ntex-ratelimiter` is a rate limiting middleware for the `ntex` web framework.
//! It uses the token bucket algorithm to limit the number of requests from clients
//! based on their IP address.
//!
//! ## Features
//!
//! - **Token Bucket Algorithm**: Efficiently manages request rates.
//! - **IP-based Rate Limiting**: Identifies clients by IP address, with support for `X-Forwarded-For` and `X-Real-IP` headers.
//! - **Configurable**: Allows customization of capacity, time window, and cleanup intervals.
//! - **Asynchronous Cleanup**: Periodically removes stale rate limit entries to save memory.
//! - **Response Headers**: Adds `X-RateLimit-Remaining`, `X-RateLimit-Limit`, and `X-RateLimit-Reset` headers to responses.
//! - **Customizable Error Response**: Returns a `429 Too Many Requests` JSON response when limits are exceeded.
//! - **Runtime Agnostic**: Supports both `tokio` (default) and `async-std` runtimes via feature flags.
//!
//! ## Installation
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! ntex-ratelimiter = "^0.1.0"
//! ```
//!
//! ## Feature Flags
//!
//! - `tokio` (default): Enables Tokio runtime support.
//! - `async-std`: Enables async-std runtime support.
//! - `json` (default): Enables JSON serialization for error responses using `serde`.
//!
//! Example with `async-std` and `json`:
//! ```toml
//! [dependencies]
//! ntex-ratelimiter = { version = "^0.1.0", default-features = false, features = ["async-std", "json"] }
//! ```
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use ntex::web;
//! use ntex_ratelimiter::{RateLimit, RateLimiter};
//!
//! #[ntex::main]
//! async fn main() -> std::io::Result<()> {
//! // Create a rate limiter: 100 requests per 60 seconds
//! let limiter = RateLimiter::new(100, 60);
//!
//! web::HttpServer::new(move || {
//! web::App::new()
//! // Apply rate limiting middleware
//! .wrap(RateLimit::new(limiter.clone()))
//! .service(web::resource("/").to(|| async { "Hello world!" }))
//! })
//! .bind("127.0.0.1:8080")?
//! .run()
//! .await
//! }
//! ```
//!
//! For more advanced usage and configuration, please refer to the documentation of
//! [`RateLimiter`] and [`RateLimiterConfig`].
//!
//! ## Module Structure
//!
//! - `limiter`: Contains the core rate limiting logic, including the `RateLimiter` struct,
//! `RateLimiterConfig`, `TokenBucket` implementation, and the `RateLimit` ntex middleware.
pub use ;
// Prevent conflicting runtime features
compile_error!;
compile_error!;
pub use tokio;
pub use async_std;