forked from solutions-plug/predictIQ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate_limit.rs
More file actions
199 lines (179 loc) · 5.82 KB
/
Copy pathrate_limit.rs
File metadata and controls
199 lines (179 loc) · 5.82 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Redis-backed rate limiting for predictIQ API.
//!
//! Uses a sliding window algorithm implemented with Redis atomic Lua script
//! to persist counters across service restarts and horizontal scaling.
//!
//! ## Algorithm
//! For each (key, window) pair:
//! 1. ZADD with current timestamp as score
//! 2. ZREMRANGEBYSCORE to evict expired entries outside the window
//! 3. ZCARD to count requests in window
//! 4. EXPIRE to align Redis TTL with the window
//!
//! All four commands execute atomically via a Lua script.
use axum::{
extract::State,
http::{HeaderMap, StatusCode},
middleware::Next,
response::{IntoResponse, Response},
Json,
};
use deadpool_redis::{Pool as RedisPool, redis::AsyncCommands};
use serde::Serialize;
use std::time::{SystemTime, UNIX_EPOCH};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct RateLimitConfig {
pub max_requests: u64,
pub window_seconds: u64,
pub key_prefix: String,
}
impl Default for RateLimitConfig {
fn default() -> Self {
Self {
max_requests: 100,
window_seconds: 60,
key_prefix: "ratelimit".to_string(),
}
}
}
#[derive(Clone)]
pub struct RateLimitState {
pub redis: Arc<RedisPool>,
pub config: RateLimitConfig,
/// Optional metrics sink. When present, rejections are counted under
/// the `rate_limit_rejections_total` Prometheus counter.
pub metrics: Option<crate::metrics::Metrics>,
}
#[derive(Serialize)]
struct RateLimitError {
error: &'static str,
message: String,
retry_after: u64,
}
// KEYS[1] = rate limit key
// ARGV[1] = current timestamp (ms)
// ARGV[2] = window start (ms)
// ARGV[3] = window TTL (seconds)
// ARGV[4] = unique member
// Returns request count within the current window.
const SLIDING_WINDOW_SCRIPT: &str = r#"
local key = KEYS[1]
local now = tonumber(ARGV[1])
local window_start = tonumber(ARGV[2])
local ttl = tonumber(ARGV[3])
local member = ARGV[4]
redis.call('ZADD', key, now, member)
redis.call('ZREMRANGEBYSCORE', key, '-inf', window_start)
local count = redis.call('ZCARD', key)
redis.call('EXPIRE', key, ttl)
return count
"#;
pub async fn check_rate_limit(
redis: &RedisPool,
config: &RateLimitConfig,
client_key: &str,
) -> Result<u64, u64> {
let mut conn = redis
.get()
.await
.map_err(|_| config.window_seconds)?;
let now_ms: u64 = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
let window_start_ms = now_ms.saturating_sub(config.window_seconds * 1_000);
let member = format!("{}-{}", now_ms, fastrand::u64(..));
let redis_key = format!("{}:{}:{}", config.key_prefix, client_key, config.window_seconds);
let script = deadpool_redis::redis::Script::new(SLIDING_WINDOW_SCRIPT);
let count: u64 = script
.key(&redis_key)
.arg(now_ms)
.arg(window_start_ms)
.arg(config.window_seconds + 1)
.arg(&member)
.invoke_async(&mut conn)
.await
.map_err(|_| config.window_seconds)?;
if count > config.max_requests {
Err(config.window_seconds)
} else {
Ok(count)
}
}
pub async fn rate_limit_middleware(
State(state): State<RateLimitState>,
headers: HeaderMap,
req: axum::extract::Request,
next: Next,
) -> Response {
let client_key = client_key_from_headers(&headers);
match check_rate_limit(&state.redis, &state.config, &client_key).await {
Ok(_count) => next.run(req).await,
Err(retry_after) => {
if let Some(m) = &state.metrics {
m.observe_rate_limit_rejection(&state.config.key_prefix);
}
tracing::warn!(
client_key = %client_key,
route = %state.config.key_prefix,
retry_after,
"rate limit exceeded"
);
let body = RateLimitError {
error: "rate_limit_exceeded",
message: format!(
"Rate limit of {} requests per {}s exceeded. Retry after {} seconds.",
state.config.max_requests, state.config.window_seconds, retry_after
),
retry_after,
};
(
StatusCode::TOO_MANY_REQUESTS,
[("Retry-After", retry_after.to_string())],
Json(body),
)
.into_response()
}
}
}
fn client_key_from_headers(headers: &HeaderMap) -> String {
headers
.get("x-forwarded-for")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.split(',').next())
.map(|s| s.trim().to_string())
.unwrap_or_else(|| "unknown".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn client_key_from_x_forwarded_for_picks_first_ip() {
let mut headers = HeaderMap::new();
headers.insert("x-forwarded-for", "1.2.3.4, 5.6.7.8".parse().unwrap());
assert_eq!(client_key_from_headers(&headers), "1.2.3.4");
}
#[test]
fn client_key_falls_back_to_unknown() {
let headers = HeaderMap::new();
assert_eq!(client_key_from_headers(&headers), "unknown");
}
#[test]
fn config_defaults_are_sensible() {
let cfg = RateLimitConfig::default();
assert_eq!(cfg.max_requests, 100);
assert_eq!(cfg.window_seconds, 60);
assert!(!cfg.key_prefix.is_empty());
}
#[test]
fn rate_limit_state_metrics_field_is_optional() {
let state = RateLimitState {
redis: std::sync::Arc::new(deadpool_redis::Config::from_url("redis://127.0.0.1")
.create_pool(Some(deadpool_redis::Runtime::Tokio1)).unwrap()),
config: RateLimitConfig::default(),
metrics: None,
};
assert!(state.metrics.is_none());
}
}