1use crate::error::Result;
10use crate::rate_limit::RateLimitResult;
11use chrono::{DateTime, Duration, Timelike, Utc};
12use parking_lot::RwLock;
13use serde::{Deserialize, Serialize};
14use std::collections::HashMap;
15use std::sync::Arc;
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct AdaptiveRateLimitConfig {
20 pub enabled: bool,
22
23 pub min_rate_limit: u32,
25
26 pub max_rate_limit: u32,
28
29 pub learning_window_hours: i64,
31
32 pub adjustment_factor: f64,
34
35 pub enable_anomaly_throttling: bool,
37
38 pub enable_load_based_adjustment: bool,
40
41 pub enable_pattern_prediction: bool,
43}
44
45impl Default for AdaptiveRateLimitConfig {
46 fn default() -> Self {
47 Self {
48 enabled: true,
49 min_rate_limit: 10,
50 max_rate_limit: 10_000,
51 learning_window_hours: 24 * 7, adjustment_factor: 0.3,
53 enable_anomaly_throttling: true,
54 enable_load_based_adjustment: true,
55 enable_pattern_prediction: true,
56 }
57 }
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
62struct TenantUsageProfile {
63 tenant_id: String,
64
65 hourly_averages: Vec<f64>, daily_averages: Vec<f64>, peak_times: Vec<u32>, avg_requests_per_hour: f64,
72 stddev_requests_per_hour: f64,
73 max_requests_per_hour: f64,
74
75 current_limit: u32,
77 base_limit: u32,
78 adjustment_history: Vec<LimitAdjustment>,
79
80 last_updated: DateTime<Utc>,
82 data_points: usize,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86struct LimitAdjustment {
87 timestamp: DateTime<Utc>,
88 old_limit: u32,
89 new_limit: u32,
90 reason: AdjustmentReason,
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
94enum AdjustmentReason {
95 NormalLearning,
96 AnomalyDetected,
97 HighLoad,
98 AttackMitigation,
99 PatternPrediction,
100}
101
102#[derive(Debug, Clone)]
104pub struct SystemLoad {
105 pub cpu_usage: f64,
106 pub memory_usage: f64,
107 pub active_connections: usize,
108 pub queue_depth: usize,
109}
110
111pub struct AdaptiveRateLimiter {
113 config: Arc<RwLock<AdaptiveRateLimitConfig>>,
114
115 profiles: Arc<RwLock<HashMap<String, TenantUsageProfile>>>,
117
118 recent_requests: Arc<RwLock<Vec<RequestRecord>>>,
120
121 load_history: Arc<RwLock<Vec<(DateTime<Utc>, SystemLoad)>>>,
123}
124
125#[derive(Debug, Clone)]
126struct RequestRecord {
127 tenant_id: String,
128 timestamp: DateTime<Utc>,
129 allowed: bool,
130 cost: f64,
131}
132
133impl AdaptiveRateLimiter {
134 pub fn new(config: AdaptiveRateLimitConfig) -> Self {
136 Self {
137 config: Arc::new(RwLock::new(config)),
138 profiles: Arc::new(RwLock::new(HashMap::new())),
139 recent_requests: Arc::new(RwLock::new(Vec::new())),
140 load_history: Arc::new(RwLock::new(Vec::new())),
141 }
142 }
143
144 pub fn check_adaptive_limit(&self, tenant_id: &str) -> Result<RateLimitResult> {
146 let config = self.config.read();
147
148 if !config.enabled {
149 return Ok(RateLimitResult {
150 allowed: true,
151 remaining: u32::MAX,
152 retry_after: None,
153 limit: u32::MAX,
154 });
155 }
156
157 let mut profiles = self.profiles.write();
159 let profile = profiles.entry(tenant_id.to_string()).or_insert_with(|| {
160 TenantUsageProfile::new(tenant_id.to_string(), config.max_rate_limit)
161 });
162
163 self.record_request(tenant_id, true, 1.0);
165
166 let recent = self.recent_requests.read();
168 let cutoff = Utc::now() - Duration::hours(1);
169 let recent_count = recent
170 .iter()
171 .filter(|r| r.tenant_id.as_str() == tenant_id && r.timestamp > cutoff)
172 .count();
173
174 let allowed = (recent_count as u32) < profile.current_limit;
176
177 let result = RateLimitResult {
178 allowed,
179 remaining: if allowed {
180 profile.current_limit.saturating_sub(recent_count as u32)
181 } else {
182 0
183 },
184 retry_after: if allowed {
185 None
186 } else {
187 Some(std::time::Duration::from_secs(60))
188 },
189 limit: profile.current_limit,
190 };
191
192 profile.data_points += 1;
194 profile.last_updated = Utc::now();
195
196 Ok(result)
197 }
198
199 pub fn update_adaptive_limits(&self) -> Result<()> {
201 let config = self.config.read();
202
203 if !config.enabled {
204 return Ok(());
205 }
206
207 let mut profiles = self.profiles.write();
208
209 for (tenant_id, profile) in profiles.iter_mut() {
210 if profile.data_points < 100 {
211 continue; }
213
214 let mut new_limit = profile.current_limit;
215 let mut reason = AdjustmentReason::NormalLearning;
216
217 if profile.data_points >= 1000 {
219 let usage_factor = profile.avg_requests_per_hour / profile.current_limit as f64;
220
221 if usage_factor > 0.8 {
222 new_limit =
224 ((profile.current_limit as f64) * (1.0 + config.adjustment_factor)) as u32;
225 reason = AdjustmentReason::NormalLearning;
226 } else if usage_factor < 0.3 {
227 new_limit = ((profile.current_limit as f64)
229 * (1.0 - config.adjustment_factor * 0.5))
230 as u32;
231 reason = AdjustmentReason::NormalLearning;
232 }
233 }
234
235 if config.enable_anomaly_throttling {
237 let recent = self.recent_requests.read();
238 let cutoff = Utc::now() - Duration::minutes(5);
239 let very_recent_count = recent
240 .iter()
241 .filter(|r| r.tenant_id.as_str() == tenant_id && r.timestamp > cutoff)
242 .count();
243
244 let expected_in_5min = profile.avg_requests_per_hour / 12.0;
246 if very_recent_count as f64 > expected_in_5min * 3.0 {
247 new_limit = ((profile.current_limit as f64) * 0.5) as u32;
248 reason = AdjustmentReason::AnomalyDetected;
249 }
250 }
251
252 if config.enable_load_based_adjustment {
254 if let Some(load) = self.get_current_load() {
255 if load.cpu_usage > 0.8 || load.memory_usage > 0.8 {
256 new_limit = ((profile.current_limit as f64) * 0.7) as u32;
258 reason = AdjustmentReason::HighLoad;
259 }
260 }
261 }
262
263 new_limit = new_limit.clamp(config.min_rate_limit, config.max_rate_limit);
265
266 if new_limit != profile.current_limit {
268 profile.adjustment_history.push(LimitAdjustment {
269 timestamp: Utc::now(),
270 old_limit: profile.current_limit,
271 new_limit,
272 reason,
273 });
274
275 profile.current_limit = new_limit;
276
277 if profile.adjustment_history.len() > 100 {
279 profile.adjustment_history.remove(0);
280 }
281 }
282 }
283
284 Ok(())
285 }
286
287 pub fn predict_and_adjust(&self, tenant_id: &str) -> Result<u32> {
289 let config = self.config.read();
290
291 if !config.enable_pattern_prediction {
292 return Ok(0);
293 }
294
295 let profiles = self.profiles.read();
296 if let Some(profile) = profiles.get(tenant_id) {
297 if profile.data_points < 1000 {
298 return Ok(profile.current_limit);
299 }
300
301 let current_hour = Utc::now().hour();
303
304 if profile.peak_times.contains(¤t_hour) {
305 let predicted_limit = ((profile.current_limit as f64) * 1.2) as u32;
307 return Ok(predicted_limit.min(config.max_rate_limit));
308 }
309 }
310
311 Ok(0)
312 }
313
314 pub fn record_system_load(&self, load: SystemLoad) {
316 let mut history = self.load_history.write();
317 history.push((Utc::now(), load));
318
319 let cutoff = Utc::now() - Duration::hours(1);
321 history.retain(|(ts, _)| *ts > cutoff);
322 }
323
324 fn get_current_load(&self) -> Option<SystemLoad> {
325 let history = self.load_history.read();
326 history.last().map(|(_, load)| load.clone())
327 }
328
329 fn record_request(&self, tenant_id: &str, allowed: bool, cost: f64) {
330 let mut requests = self.recent_requests.write();
331 requests.push(RequestRecord {
332 tenant_id: tenant_id.to_string(),
333 timestamp: Utc::now(),
334 allowed,
335 cost,
336 });
337
338 let cutoff = Utc::now() - Duration::hours(self.config.read().learning_window_hours);
340 requests.retain(|r| r.timestamp > cutoff);
341 }
342
343 pub fn get_tenant_stats(&self, tenant_id: &str) -> Option<AdaptiveLimitStats> {
345 let profiles = self.profiles.read();
346 profiles.get(tenant_id).map(|profile| {
347 let recent = self.recent_requests.read();
348 let cutoff = Utc::now() - Duration::hours(1);
349 let requests_last_hour = recent
350 .iter()
351 .filter(|r| r.tenant_id.as_str() == tenant_id && r.timestamp > cutoff)
352 .count();
353
354 AdaptiveLimitStats {
355 current_limit: profile.current_limit,
356 base_limit: profile.base_limit,
357 requests_last_hour: requests_last_hour as u32,
358 avg_requests_per_hour: profile.avg_requests_per_hour,
359 utilization: requests_last_hour as f64 / profile.current_limit as f64,
360 total_adjustments: profile.adjustment_history.len(),
361 last_adjustment: profile.adjustment_history.last().map(|a| a.timestamp),
362 }
363 })
364 }
365
366 pub fn get_stats(&self) -> AdaptiveRateLimiterStats {
368 let profiles = self.profiles.read();
369 let recent = self.recent_requests.read();
370
371 AdaptiveRateLimiterStats {
372 total_tenants: profiles.len(),
373 total_requests: recent.len(),
374 config: self.config.read().clone(),
375 }
376 }
377}
378
379impl TenantUsageProfile {
380 fn new(tenant_id: String, base_limit: u32) -> Self {
381 Self {
382 tenant_id,
383 hourly_averages: vec![0.0; 24],
384 daily_averages: vec![0.0; 7],
385 peak_times: Vec::new(),
386 avg_requests_per_hour: 0.0,
387 stddev_requests_per_hour: 0.0,
388 max_requests_per_hour: 0.0,
389 current_limit: base_limit,
390 base_limit,
391 adjustment_history: Vec::new(),
392 last_updated: Utc::now(),
393 data_points: 0,
394 }
395 }
396}
397
398#[derive(Debug, Clone, Serialize, Deserialize)]
399pub struct AdaptiveLimitStats {
400 pub current_limit: u32,
401 pub base_limit: u32,
402 pub requests_last_hour: u32,
403 pub avg_requests_per_hour: f64,
404 pub utilization: f64,
405 pub total_adjustments: usize,
406 pub last_adjustment: Option<DateTime<Utc>>,
407}
408
409#[derive(Debug, Clone, Serialize, Deserialize)]
410pub struct AdaptiveRateLimiterStats {
411 pub total_tenants: usize,
412 pub total_requests: usize,
413 pub config: AdaptiveRateLimitConfig,
414}
415
416#[cfg(test)]
417mod tests {
418 use super::*;
419
420 #[test]
421 fn test_adaptive_limiter_creation() {
422 let limiter = AdaptiveRateLimiter::new(AdaptiveRateLimitConfig::default());
423 let stats = limiter.get_stats();
424
425 assert_eq!(stats.total_tenants, 0);
426 assert_eq!(stats.total_requests, 0);
427 }
428
429 #[test]
430 fn test_adaptive_limit_checking() {
431 let limiter = AdaptiveRateLimiter::new(AdaptiveRateLimitConfig {
432 enabled: true,
433 min_rate_limit: 10,
434 max_rate_limit: 100,
435 ..Default::default()
436 });
437
438 let result = limiter.check_adaptive_limit("tenant1").unwrap();
440 assert!(result.allowed);
441 }
442
443 #[test]
444 fn test_limit_adjustment() {
445 let mut config = AdaptiveRateLimitConfig::default();
446 config.min_rate_limit = 10;
447 config.max_rate_limit = 1000;
448
449 let limiter = AdaptiveRateLimiter::new(config);
450
451 {
453 let mut profiles = limiter.profiles.write();
454 let mut profile = TenantUsageProfile::new("tenant1".to_string(), 100);
455 profile.data_points = 1500;
456 profile.avg_requests_per_hour = 90.0; profile.current_limit = 100;
458 profiles.insert("tenant1".to_string(), profile);
459 }
460
461 limiter.update_adaptive_limits().unwrap();
463
464 let stats = limiter.get_tenant_stats("tenant1").unwrap();
466 assert!(stats.current_limit > 100); }
468
469 #[test]
470 fn test_load_based_adjustment() {
471 let limiter = AdaptiveRateLimiter::new(AdaptiveRateLimitConfig::default());
472
473 {
475 let mut profiles = limiter.profiles.write();
476 let mut profile = TenantUsageProfile::new("tenant1".to_string(), 100);
477 profile.data_points = 1000;
478 profile.current_limit = 100;
479 profiles.insert("tenant1".to_string(), profile);
480 }
481
482 limiter.record_system_load(SystemLoad {
484 cpu_usage: 0.9,
485 memory_usage: 0.85,
486 active_connections: 1000,
487 queue_depth: 500,
488 });
489
490 limiter.update_adaptive_limits().unwrap();
492
493 let stats = limiter.get_tenant_stats("tenant1").unwrap();
495 assert!(stats.current_limit < 100); }
497
498 #[test]
499 fn test_disabled_adaptive_limiting() {
500 let mut config = AdaptiveRateLimitConfig::default();
501 config.enabled = false;
502
503 let limiter = AdaptiveRateLimiter::new(config);
504 let result = limiter.check_adaptive_limit("tenant1").unwrap();
505
506 assert!(result.allowed);
507 assert_eq!(result.remaining, u32::MAX);
508 }
509
510 #[test]
511 fn test_safety_limits() {
512 let limiter = AdaptiveRateLimiter::new(AdaptiveRateLimitConfig {
513 min_rate_limit: 50,
514 max_rate_limit: 200,
515 ..Default::default()
516 });
517
518 {
520 let mut profiles = limiter.profiles.write();
521 let mut profile = TenantUsageProfile::new("tenant1".to_string(), 100);
522 profile.data_points = 1500;
523 profile.avg_requests_per_hour = 180.0; profile.current_limit = 190;
525 profiles.insert("tenant1".to_string(), profile);
526 }
527
528 limiter.update_adaptive_limits().unwrap();
529
530 let stats = limiter.get_tenant_stats("tenant1").unwrap();
531 assert!(stats.current_limit <= 200); assert!(stats.current_limit >= 50); }
534}