Expand description
Per-peer bandwidth management with token-bucket rate limiting and fairness scheduling.
This module provides production-grade per-peer bandwidth tracking and enforcement:
- Token-bucket based rate limiting for upload and download directions
- Configurable burst sizes for bursty traffic tolerance
- Sliding-window usage snapshots for real-time rate computation
- Fairness scheduling policies (Max-Min, Weighted Fair, Unrestricted)
- Top-K upload/download peer ranking
- Idle peer eviction to bound memory
- Global aggregate statistics
§Example
use ipfrs_network::peer_bandwidth_manager::{
BandwidthManagerConfig, BandwidthLimit, BandwidthDirection, PeerBandwidthManager,
};
let config = BandwidthManagerConfig::default();
let mut mgr = PeerBandwidthManager::new(config);
mgr.register_peer("peer-1".to_string(), 0);
// Try to consume 512 bytes for upload
let allowed = mgr.try_consume("peer-1", 512, BandwidthDirection::Upload, 0);
assert!(allowed);
// Record an unconditional transfer
mgr.record_transfer("peer-1", 512, 256, 1_000);
// Query usage
if let Some(usage) = mgr.peer_usage("peer-1", 5_000) {
println!("upload rate: {:.1} bps", usage.upload_rate_bps);
}Structs§
- Bandwidth
Limit - Token-bucket parameters for a single bandwidth direction.
- Bandwidth
Manager Config - Configuration for
PeerBandwidthManager. - Bandwidth
Manager Stats - Aggregate statistics for the entire
PeerBandwidthManager. - Bandwidth
Usage - Computed bandwidth usage for a peer within the configured time window.
- Peer
Bandwidth Manager - Manages per-peer bandwidth state, rate limiting, and fairness scheduling.
- Peer
Bandwidth State - Token-bucket state for a single peer.
Enums§
- Bandwidth
Direction - Direction of bandwidth consumption for rate-limiting purposes.
- Fairness
Policy - Scheduling policy used by
PeerBandwidthManager::apply_fairness.