Skip to main content

Module bandwidth_budget

Module bandwidth_budget 

Source
Expand description

Per-peer bandwidth budget allocation and enforcement with token-bucket per peer.

Each peer gets an independent token bucket for upload and download. Tokens refill over time at the configured rate (bytes/sec) and may briefly exceed the nominal quota up to quota * burst_factor to allow short bursts.

A global upload and download cap prevents any single peer’s activity from starving the whole node.

§Example

use ipfrs_network::bandwidth_budget::{
    BandwidthBudgetManager, BandwidthQuota, BudgetConfig,
};

let quota = BandwidthQuota {
    upload_bytes_per_sec: 1_000_000,
    download_bytes_per_sec: 2_000_000,
    burst_factor: 1.5,
};
let config = BudgetConfig {
    default_quota: quota,
    global_upload_cap: 100_000_000,
    global_download_cap: 200_000_000,
    min_tokens: 0.0,
    gc_threshold: 1000,
};
let mut mgr = BandwidthBudgetManager::new(config);

// Register a peer with timestamp 0 (ms since epoch or any monotonic counter).
mgr.register_peer("peer-1", 0);

// Try to use 512 KiB of upload bandwidth at t=1000 ms.
let allowed = mgr.try_consume_upload("peer-1", 512 * 1024, 1000);
println!("upload allowed: {allowed}");

Structs§

BandwidthBudgetManager
Per-peer token-bucket bandwidth manager.
BandwidthQuota
Steady-state rate limits for a single peer.
BudgetConfig
Global configuration for BandwidthBudgetManager.
BudgetStats
Aggregate statistics collected by BandwidthBudgetManager.
PeerBucket
Token-bucket state for a single peer.