pub struct BandwidthMonitor { /* private fields */ }Expand description
Monitor that tracks per-peer and aggregate bandwidth usage.
BandwidthMonitor maintains a map of PeerBandwidth entries protected
by a RwLock. All mutating operations take a write lock; read-only
queries take a read lock. Global byte totals are accumulated in
BandwidthStats using lock-free atomics, so callers can take cheap
snapshots without acquiring the peer map lock.
The sliding window used for rate calculations is configurable at
construction time (defaults to 10 seconds). Samples older than the
window are lazily evicted on each call to record.
Implementations§
Source§impl BandwidthMonitor
impl BandwidthMonitor
Sourcepub fn with_window(window: Duration) -> Self
pub fn with_window(window: Duration) -> Self
Create a new monitor with a custom sliding window duration.
Sourcepub fn stats(&self) -> &Arc<BandwidthStats> ⓘ
pub fn stats(&self) -> &Arc<BandwidthStats> ⓘ
Return a shared reference to the global atomic stats.
Sourcepub fn record(&self, peer_id: &str, bytes: u64, direction: Direction)
pub fn record(&self, peer_id: &str, bytes: u64, direction: Direction)
Record a transfer of bytes in direction for peer_id.
This method:
- Obtains a write lock on the peer map.
- Creates a
PeerBandwidthentry if one does not exist. - Appends a new sample stamped with the current instant.
- Evicts samples older than the sliding window from that peer.
- Increments the global atomic counters.
Sourcepub fn rate_for_peer(&self, peer_id: &str, direction: Direction) -> f64
pub fn rate_for_peer(&self, peer_id: &str, direction: Direction) -> f64
Return the current rate (bytes/sec) for peer_id in direction.
Returns 0.0 if the peer is unknown.
Sourcepub fn top_senders(&self, n: usize) -> Vec<(String, f64)>
pub fn top_senders(&self, n: usize) -> Vec<(String, f64)>
Return the top n peers by outbound rate, sorted descending.
Each element is (peer_id, rate_bps). If there are fewer than n
peers, all known peers are returned.
Sourcepub fn top_receivers(&self, n: usize) -> Vec<(String, f64)>
pub fn top_receivers(&self, n: usize) -> Vec<(String, f64)>
Return the top n peers by inbound rate, sorted descending.
Each element is (peer_id, rate_bps).
Sourcepub fn total_rate_bps(&self, direction: Direction) -> f64
pub fn total_rate_bps(&self, direction: Direction) -> f64
Compute the aggregate rate (bytes/sec) across all peers for direction.
Sourcepub fn evict_idle_peers(&self, max_idle: Duration)
pub fn evict_idle_peers(&self, max_idle: Duration)
Remove peers whose most recent sample is older than max_idle.
A peer is considered idle when:
- It has no samples at all, or
- Its most recent sample (in either direction) was recorded more than
max_idleago.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Return the current number of tracked peers.
Sourcepub fn stats_snapshot(&self) -> BandwidthStatsSnapshot
pub fn stats_snapshot(&self) -> BandwidthStatsSnapshot
Take a snapshot of the global atomic stats without acquiring the peer map lock.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for BandwidthMonitor
impl !RefUnwindSafe for BandwidthMonitor
impl Send for BandwidthMonitor
impl Sync for BandwidthMonitor
impl Unpin for BandwidthMonitor
impl UnsafeUnpin for BandwidthMonitor
impl UnwindSafe for BandwidthMonitor
Blanket Implementations§
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