pub struct AdaptiveBandwidthAllocator { /* private fields */ }Expand description
Adaptive bandwidth manager that dynamically allocates bandwidth across peers.
Tracks per-peer profiles, maintains a bounded event history, and supports
five allocation policies via AdaptiveBandwidthAllocator::reallocate.
Implementations§
Source§impl AdaptiveBandwidthAllocator
impl AdaptiveBandwidthAllocator
Sourcepub const MAX_EVENT_HISTORY: usize = 200
pub const MAX_EVENT_HISTORY: usize = 200
Maximum number of events retained in the history ring-buffer.
Sourcepub fn new(config: AllocatorConfig) -> Self
pub fn new(config: AllocatorConfig) -> Self
Create a new allocator with the given configuration.
Returns AllocatorError::InvalidConfiguration if the config is logically
inconsistent.
Sourcepub fn advance_time(&mut self, delta_ms: u64)
pub fn advance_time(&mut self, delta_ms: u64)
Advance the internal clock by delta_ms milliseconds.
Useful in testing to create distinct timestamps without real-time delays.
Sourcepub fn add_peer(
&mut self,
peer_id: String,
class: BandwidthClass,
initial_bps: u64,
) -> Result<(), AllocatorError>
pub fn add_peer( &mut self, peer_id: String, class: BandwidthClass, initial_bps: u64, ) -> Result<(), AllocatorError>
Add a peer with an initial bandwidth class and desired allocation.
Returns AllocatorError::AllocationExceedsCapacity if adding this
allocation would exceed total_capacity_bps.
Sourcepub fn remove_peer(
&mut self,
peer_id: &str,
) -> Result<PeerBandwidthProfile, AllocatorError>
pub fn remove_peer( &mut self, peer_id: &str, ) -> Result<PeerBandwidthProfile, AllocatorError>
Remove a peer and release its bandwidth allocation.
Returns the removed PeerBandwidthProfile or
AllocatorError::PeerNotFound.
Sourcepub fn update_measurement(
&mut self,
peer_id: &str,
used_bps: u64,
latency_ms: u64,
packet_loss_ppm: u32,
) -> Result<BandwidthEvent, AllocatorError>
pub fn update_measurement( &mut self, peer_id: &str, used_bps: u64, latency_ms: u64, packet_loss_ppm: u32, ) -> Result<BandwidthEvent, AllocatorError>
Record a live measurement for a peer.
Updates the rolling BandwidthWindow, stores latency and loss, and
emits a BandwidthEvent::CongestionDetected when loss_ppm exceeds the
configured threshold.
Returns the most pertinent event for the caller’s convenience.
Sourcepub fn reallocate(&mut self) -> Vec<BandwidthEvent>
pub fn reallocate(&mut self) -> Vec<BandwidthEvent>
Rebalance all peer allocations according to the configured policy.
Returns the list of BandwidthEvents produced (one
AllocationRevised event at minimum, plus any CongestionDetected
events for peers that are still over threshold after rebalancing).
Sourcepub fn get_allocation(&self, peer_id: &str) -> Result<u64, AllocatorError>
pub fn get_allocation(&self, peer_id: &str) -> Result<u64, AllocatorError>
Return the current allocated bps for the given peer.
Sourcepub fn stats(&self) -> BandwidthStats
pub fn stats(&self) -> BandwidthStats
Return aggregate statistics including Jain’s fairness index.
Sourcepub fn congested_peers(&self) -> Vec<String>
pub fn congested_peers(&self) -> Vec<String>
Return peer IDs for all peers whose packet_loss_ppm exceeds the threshold.
Sourcepub fn events_since(&self, since_ms: u64) -> Vec<BandwidthEvent>
pub fn events_since(&self, since_ms: u64) -> Vec<BandwidthEvent>
Return all events with timestamp >= since_ms.
Sourcepub fn drain_events(&mut self) -> Vec<BandwidthEvent>
pub fn drain_events(&mut self) -> Vec<BandwidthEvent>
Drain and return all buffered events, leaving the history empty.
Sourcepub fn peer_profile(&self, peer_id: &str) -> Option<&PeerBandwidthProfile>
pub fn peer_profile(&self, peer_id: &str) -> Option<&PeerBandwidthProfile>
Return a reference to the profile for peer_id, if present.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Return the number of tracked peers.
Sourcepub fn total_capacity_bps(&self) -> u64
pub fn total_capacity_bps(&self) -> u64
Return the total capacity configured for this allocator.
Sourcepub fn config(&self) -> &AllocatorConfig
pub fn config(&self) -> &AllocatorConfig
Return a reference to the current configuration.
Auto Trait Implementations§
impl Freeze for AdaptiveBandwidthAllocator
impl RefUnwindSafe for AdaptiveBandwidthAllocator
impl Send for AdaptiveBandwidthAllocator
impl Sync for AdaptiveBandwidthAllocator
impl Unpin for AdaptiveBandwidthAllocator
impl UnsafeUnpin for AdaptiveBandwidthAllocator
impl UnwindSafe for AdaptiveBandwidthAllocator
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more