rs3gw 0.2.1

High-Performance AI/HPC Object Storage Gateway powered by scirs2-io
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Bandwidth throttling and rate limiting
//!
//! Provides per-client and per-bucket bandwidth throttling using token bucket algorithm.

use axum::http::Request;
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, warn};

/// Throttle configuration
#[derive(Debug, Clone)]
pub struct ThrottleConfig {
    /// Max bytes per second for uploads (0 = unlimited)
    pub upload_bytes_per_sec: u64,
    /// Max bytes per second for downloads (0 = unlimited)
    pub download_bytes_per_sec: u64,
    /// Max requests per second (0 = unlimited)
    pub requests_per_sec: u32,
    /// Burst allowance multiplier (e.g., 2.0 allows 2x burst)
    pub burst_multiplier: f64,
    /// Time window for rate calculation
    pub window_secs: u64,
}

impl Default for ThrottleConfig {
    fn default() -> Self {
        Self {
            upload_bytes_per_sec: 0,   // Unlimited
            download_bytes_per_sec: 0, // Unlimited
            requests_per_sec: 0,       // Unlimited
            burst_multiplier: 2.0,
            window_secs: 1,
        }
    }
}

impl ThrottleConfig {
    /// Create config with upload limit in MB/s
    pub fn with_upload_mbps(mut self, mbps: u64) -> Self {
        self.upload_bytes_per_sec = mbps * 1024 * 1024;
        self
    }

    /// Create config with download limit in MB/s
    pub fn with_download_mbps(mut self, mbps: u64) -> Self {
        self.download_bytes_per_sec = mbps * 1024 * 1024;
        self
    }

    /// Create config with request rate limit
    pub fn with_requests_per_sec(mut self, rps: u32) -> Self {
        self.requests_per_sec = rps;
        self
    }

    /// Check if any limits are configured
    pub fn has_limits(&self) -> bool {
        self.upload_bytes_per_sec > 0
            || self.download_bytes_per_sec > 0
            || self.requests_per_sec > 0
    }
}

/// Token bucket for rate limiting
#[derive(Debug)]
struct TokenBucket {
    /// Tokens currently available
    tokens: f64,
    /// Maximum tokens (capacity)
    capacity: f64,
    /// Tokens added per second
    refill_rate: f64,
    /// Last refill time
    last_refill: Instant,
}

impl TokenBucket {
    fn new(capacity: f64, refill_rate: f64) -> Self {
        Self {
            tokens: capacity,
            capacity,
            refill_rate,
            last_refill: Instant::now(),
        }
    }

    /// Refill tokens based on elapsed time
    fn refill(&mut self) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.last_refill).as_secs_f64();
        let new_tokens = elapsed * self.refill_rate;
        self.tokens = (self.tokens + new_tokens).min(self.capacity);
        self.last_refill = now;
    }

    /// Try to consume tokens, returns true if successful
    fn try_consume(&mut self, amount: f64) -> bool {
        self.refill();
        if self.tokens >= amount {
            self.tokens -= amount;
            true
        } else {
            false
        }
    }

    /// Get time until tokens are available
    fn time_until_available(&mut self, amount: f64) -> Duration {
        self.refill();
        if self.tokens >= amount {
            Duration::ZERO
        } else {
            let needed = amount - self.tokens;
            Duration::from_secs_f64(needed / self.refill_rate)
        }
    }
}

/// Throttle state for a single client
#[derive(Debug)]
struct ClientThrottle {
    /// Request rate bucket
    request_bucket: TokenBucket,
    /// Upload bandwidth bucket
    upload_bucket: TokenBucket,
    /// Download bandwidth bucket
    download_bucket: TokenBucket,
    /// Last activity time
    last_activity: Instant,
}

impl ClientThrottle {
    fn new(config: &ThrottleConfig) -> Self {
        let request_capacity = if config.requests_per_sec > 0 {
            (config.requests_per_sec as f64) * config.burst_multiplier
        } else {
            f64::MAX
        };

        let upload_capacity = if config.upload_bytes_per_sec > 0 {
            (config.upload_bytes_per_sec as f64) * config.burst_multiplier
        } else {
            f64::MAX
        };

        let download_capacity = if config.download_bytes_per_sec > 0 {
            (config.download_bytes_per_sec as f64) * config.burst_multiplier
        } else {
            f64::MAX
        };

        Self {
            request_bucket: TokenBucket::new(request_capacity, config.requests_per_sec as f64),
            upload_bucket: TokenBucket::new(upload_capacity, config.upload_bytes_per_sec as f64),
            download_bucket: TokenBucket::new(
                download_capacity,
                config.download_bytes_per_sec as f64,
            ),
            last_activity: Instant::now(),
        }
    }
}

/// Result of a throttle check
#[derive(Debug, Clone)]
pub enum ThrottleResult {
    /// Request allowed
    Allowed,
    /// Request rate limited, retry after duration
    RateLimited { retry_after: Duration },
    /// Bandwidth limited for upload
    UploadLimited { retry_after: Duration },
    /// Bandwidth limited for download
    DownloadLimited { retry_after: Duration },
}

impl ThrottleResult {
    pub fn is_allowed(&self) -> bool {
        matches!(self, Self::Allowed)
    }

    pub fn retry_after(&self) -> Option<Duration> {
        match self {
            Self::Allowed => None,
            Self::RateLimited { retry_after }
            | Self::UploadLimited { retry_after }
            | Self::DownloadLimited { retry_after } => Some(*retry_after),
        }
    }
}

/// Bandwidth throttle manager
pub struct ThrottleManager {
    /// Default config for clients
    default_config: ThrottleConfig,
    /// Per-bucket configs
    bucket_configs: Arc<RwLock<HashMap<String, ThrottleConfig>>>,
    /// Per-client state (by IP)
    client_state: Arc<RwLock<HashMap<IpAddr, ClientThrottle>>>,
    /// Per-bucket state
    bucket_state: Arc<RwLock<HashMap<String, ClientThrottle>>>,
    /// Cleanup interval for idle clients
    cleanup_interval: Duration,
    /// Max idle time before cleanup
    max_idle: Duration,
}

impl ThrottleManager {
    /// Create a new throttle manager
    pub fn new(default_config: ThrottleConfig) -> Self {
        let manager = Self {
            default_config,
            bucket_configs: Arc::new(RwLock::new(HashMap::new())),
            client_state: Arc::new(RwLock::new(HashMap::new())),
            bucket_state: Arc::new(RwLock::new(HashMap::new())),
            cleanup_interval: Duration::from_secs(60),
            max_idle: Duration::from_secs(300),
        };

        // Start cleanup task
        manager.start_cleanup_task();
        manager
    }

    fn start_cleanup_task(&self) {
        let client_state = Arc::clone(&self.client_state);
        let bucket_state = Arc::clone(&self.bucket_state);
        let max_idle = self.max_idle;
        let interval = self.cleanup_interval;

        tokio::spawn(async move {
            loop {
                tokio::time::sleep(interval).await;

                // Clean up idle clients
                {
                    let mut clients = client_state.write().await;
                    let before = clients.len();
                    clients.retain(|_, state| state.last_activity.elapsed() < max_idle);
                    let removed = before - clients.len();
                    if removed > 0 {
                        debug!(removed, "Cleaned up idle client throttle states");
                    }
                }

                // Clean up idle buckets
                {
                    let mut buckets = bucket_state.write().await;
                    let before = buckets.len();
                    buckets.retain(|_, state| state.last_activity.elapsed() < max_idle);
                    let removed = before - buckets.len();
                    if removed > 0 {
                        debug!(removed, "Cleaned up idle bucket throttle states");
                    }
                }
            }
        });
    }

    /// Set throttle config for a bucket
    pub async fn set_bucket_config(&self, bucket: &str, config: ThrottleConfig) {
        let mut configs = self.bucket_configs.write().await;
        configs.insert(bucket.to_string(), config);
    }

    /// Get throttle config for a bucket
    pub async fn get_bucket_config(&self, bucket: &str) -> ThrottleConfig {
        let configs = self.bucket_configs.read().await;
        configs
            .get(bucket)
            .cloned()
            .unwrap_or_else(|| self.default_config.clone())
    }

    /// Check if a request is allowed (rate limiting)
    pub async fn check_request(&self, client_ip: IpAddr) -> ThrottleResult {
        if self.default_config.requests_per_sec == 0 {
            return ThrottleResult::Allowed;
        }

        let mut clients = self.client_state.write().await;
        let state = clients
            .entry(client_ip)
            .or_insert_with(|| ClientThrottle::new(&self.default_config));

        state.last_activity = Instant::now();

        if state.request_bucket.try_consume(1.0) {
            ThrottleResult::Allowed
        } else {
            let retry_after = state.request_bucket.time_until_available(1.0);
            warn!(client = %client_ip, retry_after = ?retry_after, "Request rate limited");
            ThrottleResult::RateLimited { retry_after }
        }
    }

    /// Check if upload bandwidth is available
    pub async fn check_upload(
        &self,
        client_ip: IpAddr,
        bucket: &str,
        bytes: u64,
    ) -> ThrottleResult {
        let config = self.get_bucket_config(bucket).await;
        if config.upload_bytes_per_sec == 0 {
            return ThrottleResult::Allowed;
        }

        let mut buckets = self.bucket_state.write().await;
        let state = buckets
            .entry(bucket.to_string())
            .or_insert_with(|| ClientThrottle::new(&config));

        state.last_activity = Instant::now();

        if state.upload_bucket.try_consume(bytes as f64) {
            ThrottleResult::Allowed
        } else {
            let retry_after = state.upload_bucket.time_until_available(bytes as f64);
            warn!(
                client = %client_ip,
                bucket = %bucket,
                bytes,
                retry_after = ?retry_after,
                "Upload bandwidth limited"
            );
            ThrottleResult::UploadLimited { retry_after }
        }
    }

    /// Check if download bandwidth is available
    pub async fn check_download(
        &self,
        client_ip: IpAddr,
        bucket: &str,
        bytes: u64,
    ) -> ThrottleResult {
        let config = self.get_bucket_config(bucket).await;
        if config.download_bytes_per_sec == 0 {
            return ThrottleResult::Allowed;
        }

        let mut buckets = self.bucket_state.write().await;
        let state = buckets
            .entry(bucket.to_string())
            .or_insert_with(|| ClientThrottle::new(&config));

        state.last_activity = Instant::now();

        if state.download_bucket.try_consume(bytes as f64) {
            ThrottleResult::Allowed
        } else {
            let retry_after = state.download_bucket.time_until_available(bytes as f64);
            warn!(
                client = %client_ip,
                bucket = %bucket,
                bytes,
                retry_after = ?retry_after,
                "Download bandwidth limited"
            );
            ThrottleResult::DownloadLimited { retry_after }
        }
    }

    /// Extract client IP from request
    pub fn extract_client_ip<B>(req: &Request<B>) -> Option<IpAddr> {
        // Try X-Forwarded-For header first
        if let Some(xff) = req.headers().get("x-forwarded-for") {
            if let Ok(s) = xff.to_str() {
                if let Some(first) = s.split(',').next() {
                    if let Ok(ip) = first.trim().parse() {
                        return Some(ip);
                    }
                }
            }
        }

        // Try X-Real-IP header
        if let Some(xri) = req.headers().get("x-real-ip") {
            if let Ok(s) = xri.to_str() {
                if let Ok(ip) = s.parse() {
                    return Some(ip);
                }
            }
        }

        // Fall back to connection info (would need to be passed separately)
        None
    }

    /// Get current stats
    pub async fn stats(&self) -> ThrottleStats {
        let clients = self.client_state.read().await;
        let buckets = self.bucket_state.read().await;
        let configs = self.bucket_configs.read().await;

        ThrottleStats {
            active_clients: clients.len(),
            active_buckets: buckets.len(),
            configured_buckets: configs.len(),
        }
    }
}

/// Throttle statistics
#[derive(Debug, Clone)]
pub struct ThrottleStats {
    pub active_clients: usize,
    pub active_buckets: usize,
    pub configured_buckets: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_throttle_config() {
        let config = ThrottleConfig::default()
            .with_upload_mbps(100)
            .with_download_mbps(200)
            .with_requests_per_sec(1000);

        assert_eq!(config.upload_bytes_per_sec, 100 * 1024 * 1024);
        assert_eq!(config.download_bytes_per_sec, 200 * 1024 * 1024);
        assert_eq!(config.requests_per_sec, 1000);
        assert!(config.has_limits());
    }

    #[test]
    fn test_token_bucket() {
        let mut bucket = TokenBucket::new(10.0, 5.0);

        // Should have 10 tokens initially
        assert!(bucket.try_consume(5.0));
        assert!(bucket.try_consume(5.0));
        assert!(!bucket.try_consume(1.0)); // Empty

        // Wait for refill
        std::thread::sleep(Duration::from_millis(500));
        bucket.refill();
        assert!(bucket.tokens >= 2.0); // ~2.5 tokens after 0.5s at 5/s
    }

    #[tokio::test]
    async fn test_throttle_manager_unlimited() {
        let config = ThrottleConfig::default(); // No limits
        let manager = ThrottleManager::new(config);

        let ip: IpAddr = "127.0.0.1".parse().expect("Failed to parse IP address");

        // Should always be allowed when unlimited
        let result = manager.check_request(ip).await;
        assert!(result.is_allowed());

        let result = manager.check_upload(ip, "bucket", 1_000_000).await;
        assert!(result.is_allowed());
    }

    #[tokio::test]
    async fn test_throttle_manager_rate_limit() {
        let config = ThrottleConfig::default().with_requests_per_sec(2);
        let manager = ThrottleManager::new(config);

        let ip: IpAddr = "127.0.0.1".parse().expect("Failed to parse IP address");

        // First few should be allowed (burst)
        assert!(manager.check_request(ip).await.is_allowed());
        assert!(manager.check_request(ip).await.is_allowed());
        assert!(manager.check_request(ip).await.is_allowed());
        assert!(manager.check_request(ip).await.is_allowed());

        // Should eventually be limited
        let mut limited = false;
        for _ in 0..10 {
            if !manager.check_request(ip).await.is_allowed() {
                limited = true;
                break;
            }
        }
        assert!(limited, "Should have been rate limited");
    }

    #[tokio::test]
    async fn test_per_bucket_config() {
        let default_config = ThrottleConfig::default();
        let manager = ThrottleManager::new(default_config);

        // Set bucket-specific config
        let bucket_config = ThrottleConfig::default().with_upload_mbps(10);
        manager
            .set_bucket_config("limited-bucket", bucket_config)
            .await;

        // Default bucket should be unlimited
        let default = manager.get_bucket_config("other-bucket").await;
        assert_eq!(default.upload_bytes_per_sec, 0);

        // Limited bucket should have config
        let limited = manager.get_bucket_config("limited-bucket").await;
        assert_eq!(limited.upload_bytes_per_sec, 10 * 1024 * 1024);
    }
}