ipfrs-transport 0.2.0

Transport protocols and zero-copy data exchange for IPFRS distributed system
Documentation
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Bandwidth throttling for rate limiting
//!
//! Provides token bucket and leaky bucket algorithms for:
//! - Per-peer rate limits
//! - Global bandwidth caps
//! - QoS prioritization
//! - Burst allowance
//!
//! # Example
//!
//! ```
//! use ipfrs_transport::{TokenBucket, BandwidthThrottle, BandwidthConfig, QosPriority};
//!
//! // Create a token bucket with 1000 token capacity, refilling at 100 tokens/sec
//! let bucket = TokenBucket::new(1000, 100.0);
//!
//! // Try to consume 50 tokens
//! if bucket.try_consume(50) {
//!     println!("Request allowed");
//! } else {
//!     println!("Rate limit exceeded");
//! }
//!
//! // Create a bandwidth throttle with global limits
//! let mut config = BandwidthConfig::default();
//! config.global_upload_limit = 10_000_000; // 10 MB/s
//! config.global_download_limit = 10_000_000;
//! let throttle = BandwidthThrottle::new(config);
//!
//! // Register a peer with high priority
//! let peer_addr = "127.0.0.1:8080".parse().unwrap();
//! throttle.register_peer(peer_addr, QosPriority::High);
//! ```

use parking_lot::RwLock;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;
use tokio::time::sleep;
use tracing::debug;

/// Throttle error types
#[derive(Error, Debug)]
pub enum ThrottleError {
    #[error("Rate limit exceeded")]
    RateLimitExceeded,

    #[error("Quota exhausted")]
    QuotaExhausted,

    #[error("Peer not found: {0}")]
    PeerNotFound(String),
}

/// Token bucket for rate limiting
#[derive(Debug, Clone)]
pub struct TokenBucket {
    /// Maximum number of tokens (bucket capacity)
    capacity: u64,
    /// Current number of tokens
    tokens: Arc<RwLock<f64>>,
    /// Token refill rate (tokens per second)
    refill_rate: f64,
    /// Last refill time
    last_refill: Arc<RwLock<Instant>>,
}

impl TokenBucket {
    /// Create a new token bucket
    pub fn new(capacity: u64, refill_rate: f64) -> Self {
        Self {
            capacity,
            tokens: Arc::new(RwLock::new(capacity as f64)),
            refill_rate,
            last_refill: Arc::new(RwLock::new(Instant::now())),
        }
    }

    /// Try to consume tokens (non-blocking)
    pub fn try_consume(&self, amount: u64) -> bool {
        self.refill();

        let mut tokens = self.tokens.write();
        if *tokens >= amount as f64 {
            *tokens -= amount as f64;
            true
        } else {
            false
        }
    }

    /// Consume tokens (blocking until available)
    pub async fn consume(&self, amount: u64) {
        loop {
            if self.try_consume(amount) {
                return;
            }

            // Calculate wait time
            let tokens = *self.tokens.read();
            let needed = amount as f64 - tokens;
            let wait_time = Duration::from_secs_f64(needed / self.refill_rate);

            sleep(wait_time.min(Duration::from_millis(100))).await;
        }
    }

    /// Refill tokens based on elapsed time
    fn refill(&self) {
        let now = Instant::now();
        let mut last_refill = self.last_refill.write();
        let elapsed = now.duration_since(*last_refill).as_secs_f64();

        if elapsed > 0.0 {
            let new_tokens = elapsed * self.refill_rate;
            let mut tokens = self.tokens.write();
            *tokens = (*tokens + new_tokens).min(self.capacity as f64);
            *last_refill = now;
        }
    }

    /// Get current token count
    pub fn available_tokens(&self) -> u64 {
        self.refill();
        *self.tokens.read() as u64
    }

    /// Get capacity
    pub fn capacity(&self) -> u64 {
        self.capacity
    }

    /// Get refill rate
    pub fn refill_rate(&self) -> f64 {
        self.refill_rate
    }
}

/// QoS priority levels for bandwidth allocation
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum QosPriority {
    /// Best effort (lowest priority)
    BestEffort = 0,
    /// Normal priority
    Normal = 1,
    /// High priority
    High = 2,
    /// Critical (highest priority)
    Critical = 3,
}

impl QosPriority {
    /// Get bandwidth share multiplier
    pub fn multiplier(&self) -> f64 {
        match self {
            QosPriority::BestEffort => 0.5,
            QosPriority::Normal => 1.0,
            QosPriority::High => 2.0,
            QosPriority::Critical => 4.0,
        }
    }
}

/// Bandwidth throttle configuration
#[derive(Debug, Clone)]
pub struct BandwidthConfig {
    /// Global upload limit (bytes per second, 0 = unlimited)
    pub global_upload_limit: u64,
    /// Global download limit (bytes per second, 0 = unlimited)
    pub global_download_limit: u64,
    /// Per-peer upload limit (bytes per second, 0 = unlimited)
    pub peer_upload_limit: u64,
    /// Per-peer download limit (bytes per second, 0 = unlimited)
    pub peer_download_limit: u64,
    /// Allow burst transfers (use full bucket capacity)
    pub allow_burst: bool,
    /// Burst capacity multiplier
    pub burst_multiplier: f64,
}

impl Default for BandwidthConfig {
    fn default() -> Self {
        Self {
            global_upload_limit: 0,   // Unlimited
            global_download_limit: 0, // Unlimited
            peer_upload_limit: 0,     // Unlimited
            peer_download_limit: 0,   // Unlimited
            allow_burst: true,
            burst_multiplier: 2.0,
        }
    }
}

/// Statistics for bandwidth throttling
#[derive(Debug, Clone, Default)]
pub struct ThrottleStats {
    /// Total bytes uploaded
    pub bytes_uploaded: u64,
    /// Total bytes downloaded
    pub bytes_downloaded: u64,
    /// Number of times throttled
    pub throttle_count: u64,
    /// Total time spent waiting
    pub total_wait_time: Duration,
    /// Current upload rate (bytes/sec)
    pub current_upload_rate: f64,
    /// Current download rate (bytes/sec)
    pub current_download_rate: f64,
}

/// Per-peer throttle state
struct PeerThrottle {
    upload: Option<TokenBucket>,
    download: Option<TokenBucket>,
    priority: QosPriority,
}

/// Bandwidth throttle manager
pub struct BandwidthThrottle {
    config: BandwidthConfig,
    global_upload: Option<Arc<TokenBucket>>,
    global_download: Option<Arc<TokenBucket>>,
    peer_throttles: Arc<RwLock<HashMap<SocketAddr, PeerThrottle>>>,
    stats: Arc<RwLock<ThrottleStats>>,
}

impl BandwidthThrottle {
    /// Create a new bandwidth throttle
    pub fn new(config: BandwidthConfig) -> Self {
        let burst_capacity_multiplier = if config.allow_burst {
            config.burst_multiplier
        } else {
            1.0
        };

        let global_upload = if config.global_upload_limit > 0 {
            Some(Arc::new(TokenBucket::new(
                (config.global_upload_limit as f64 * burst_capacity_multiplier) as u64,
                config.global_upload_limit as f64,
            )))
        } else {
            None
        };

        let global_download = if config.global_download_limit > 0 {
            Some(Arc::new(TokenBucket::new(
                (config.global_download_limit as f64 * burst_capacity_multiplier) as u64,
                config.global_download_limit as f64,
            )))
        } else {
            None
        };

        Self {
            config,
            global_upload,
            global_download,
            peer_throttles: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(RwLock::new(ThrottleStats::default())),
        }
    }

    /// Register a peer with optional priority
    pub fn register_peer(&self, addr: SocketAddr, priority: QosPriority) {
        let burst_multiplier = if self.config.allow_burst {
            self.config.burst_multiplier
        } else {
            1.0
        };

        let upload = if self.config.peer_upload_limit > 0 {
            let rate = self.config.peer_upload_limit as f64 * priority.multiplier();
            Some(TokenBucket::new((rate * burst_multiplier) as u64, rate))
        } else {
            None
        };

        let download = if self.config.peer_download_limit > 0 {
            let rate = self.config.peer_download_limit as f64 * priority.multiplier();
            Some(TokenBucket::new((rate * burst_multiplier) as u64, rate))
        } else {
            None
        };

        let throttle = PeerThrottle {
            upload,
            download,
            priority,
        };

        self.peer_throttles.write().insert(addr, throttle);
        debug!("Registered peer {} with priority {:?}", addr, priority);
    }

    /// Unregister a peer
    pub fn unregister_peer(&self, addr: &SocketAddr) {
        self.peer_throttles.write().remove(addr);
        debug!("Unregistered peer {}", addr);
    }

    /// Throttle upload (wait until bandwidth available)
    pub async fn throttle_upload(&self, addr: &SocketAddr, bytes: u64) {
        let start = Instant::now();

        // Global throttle
        if let Some(global) = &self.global_upload {
            global.consume(bytes).await;
        }

        // Per-peer throttle
        let upload_bucket = {
            let peer_throttles = self.peer_throttles.read();
            peer_throttles
                .get(addr)
                .and_then(|peer| peer.upload.clone())
        };

        if let Some(upload) = upload_bucket {
            upload.consume(bytes).await;
        }

        // Update stats
        {
            let mut stats = self.stats.write();
            stats.bytes_uploaded += bytes;
            let wait_time = start.elapsed();
            if wait_time > Duration::from_millis(1) {
                stats.throttle_count += 1;
                stats.total_wait_time += wait_time;
            }
        }
    }

    /// Throttle download (wait until bandwidth available)
    pub async fn throttle_download(&self, addr: &SocketAddr, bytes: u64) {
        let start = Instant::now();

        // Global throttle
        if let Some(global) = &self.global_download {
            global.consume(bytes).await;
        }

        // Per-peer throttle
        let download_bucket = {
            let peer_throttles = self.peer_throttles.read();
            peer_throttles
                .get(addr)
                .and_then(|peer| peer.download.clone())
        };

        if let Some(download) = download_bucket {
            download.consume(bytes).await;
        }

        // Update stats
        {
            let mut stats = self.stats.write();
            stats.bytes_downloaded += bytes;
            let wait_time = start.elapsed();
            if wait_time > Duration::from_millis(1) {
                stats.throttle_count += 1;
                stats.total_wait_time += wait_time;
            }
        }
    }

    /// Try to throttle upload (non-blocking, returns false if would block)
    pub fn try_throttle_upload(&self, addr: &SocketAddr, bytes: u64) -> bool {
        // Global throttle
        if let Some(global) = &self.global_upload {
            if !global.try_consume(bytes) {
                return false;
            }
        }

        // Per-peer throttle
        let peer_throttles = self.peer_throttles.read();
        if let Some(peer) = peer_throttles.get(addr) {
            if let Some(upload) = &peer.upload {
                if !upload.try_consume(bytes) {
                    return false;
                }
            }
        }

        // Update stats
        self.stats.write().bytes_uploaded += bytes;
        true
    }

    /// Try to throttle download (non-blocking)
    pub fn try_throttle_download(&self, addr: &SocketAddr, bytes: u64) -> bool {
        // Global throttle
        if let Some(global) = &self.global_download {
            if !global.try_consume(bytes) {
                return false;
            }
        }

        // Per-peer throttle
        let peer_throttles = self.peer_throttles.read();
        if let Some(peer) = peer_throttles.get(addr) {
            if let Some(download) = &peer.download {
                if !download.try_consume(bytes) {
                    return false;
                }
            }
        }

        // Update stats
        self.stats.write().bytes_downloaded += bytes;
        true
    }

    /// Update peer priority
    pub fn update_peer_priority(&self, addr: &SocketAddr, priority: QosPriority) {
        let mut peer_throttles = self.peer_throttles.write();
        if let Some(peer) = peer_throttles.get_mut(addr) {
            peer.priority = priority;
            debug!("Updated peer {} priority to {:?}", addr, priority);
        }
    }

    /// Get statistics
    pub fn stats(&self) -> ThrottleStats {
        self.stats.read().clone()
    }

    /// Reset statistics
    pub fn reset_stats(&self) {
        *self.stats.write() = ThrottleStats::default();
    }

    /// Get available upload bandwidth
    pub fn available_upload_bandwidth(&self) -> Option<u64> {
        self.global_upload.as_ref().map(|b| b.available_tokens())
    }

    /// Get available download bandwidth
    pub fn available_download_bandwidth(&self) -> Option<u64> {
        self.global_download.as_ref().map(|b| b.available_tokens())
    }
}

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

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

        assert_eq!(bucket.available_tokens(), 100);
        assert!(bucket.try_consume(50));
        assert_eq!(bucket.available_tokens(), 50);
        assert!(bucket.try_consume(50));
        assert_eq!(bucket.available_tokens(), 0);
        assert!(!bucket.try_consume(1));
    }

    #[tokio::test]
    async fn test_token_bucket_refill() {
        let bucket = TokenBucket::new(100, 100.0); // 100 tokens/sec

        bucket.try_consume(100);
        assert_eq!(bucket.available_tokens(), 0);

        tokio::time::sleep(Duration::from_millis(500)).await;

        // Should have refilled ~50 tokens
        let available = bucket.available_tokens();
        assert!((45..=55).contains(&available), "Got {} tokens", available);
    }

    #[test]
    fn test_qos_priority() {
        assert_eq!(QosPriority::BestEffort.multiplier(), 0.5);
        assert_eq!(QosPriority::Normal.multiplier(), 1.0);
        assert_eq!(QosPriority::High.multiplier(), 2.0);
        assert_eq!(QosPriority::Critical.multiplier(), 4.0);
    }

    #[test]
    fn test_bandwidth_config_default() {
        let config = BandwidthConfig::default();
        assert_eq!(config.global_upload_limit, 0);
        assert!(config.allow_burst);
        assert_eq!(config.burst_multiplier, 2.0);
    }

    #[tokio::test]
    async fn test_bandwidth_throttle() {
        let config = BandwidthConfig {
            global_upload_limit: 1000,   // 1000 bytes/sec
            global_download_limit: 2000, // 2000 bytes/sec
            peer_upload_limit: 0,
            peer_download_limit: 0,
            allow_burst: false,
            burst_multiplier: 1.0,
        };

        let throttle = BandwidthThrottle::new(config);
        let addr: SocketAddr = "127.0.0.1:8080"
            .parse()
            .expect("test: parse socket address");

        throttle.register_peer(addr, QosPriority::Normal);

        // Should be able to upload immediately
        assert!(throttle.try_throttle_upload(&addr, 500));

        // Stats should be updated
        let stats = throttle.stats();
        assert_eq!(stats.bytes_uploaded, 500);
    }

    #[tokio::test]
    async fn test_peer_priority() {
        let config = BandwidthConfig {
            global_upload_limit: 0,
            global_download_limit: 0,
            peer_upload_limit: 1000, // 1000 bytes/sec
            peer_download_limit: 0,
            allow_burst: false,
            burst_multiplier: 1.0,
        };

        let throttle = BandwidthThrottle::new(config);
        let addr: SocketAddr = "127.0.0.1:8080"
            .parse()
            .expect("test: parse socket address");

        // High priority peer gets 2x bandwidth
        throttle.register_peer(addr, QosPriority::High);

        // Should be able to upload more due to higher priority
        assert!(throttle.try_throttle_upload(&addr, 1000));
    }
}