Skip to main content

Module peer_bandwidth_manager

Module peer_bandwidth_manager 

Source
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§

BandwidthLimit
Token-bucket parameters for a single bandwidth direction.
BandwidthManagerConfig
Configuration for PeerBandwidthManager.
BandwidthManagerStats
Aggregate statistics for the entire PeerBandwidthManager.
BandwidthUsage
Computed bandwidth usage for a peer within the configured time window.
PeerBandwidthManager
Manages per-peer bandwidth state, rate limiting, and fairness scheduling.
PeerBandwidthState
Token-bucket state for a single peer.

Enums§

BandwidthDirection
Direction of bandwidth consumption for rate-limiting purposes.
FairnessPolicy
Scheduling policy used by PeerBandwidthManager::apply_fairness.