pub struct PeerBandwidth {
pub peer_id: String,
pub inbound_samples: VecDeque<BandwidthSample>,
pub outbound_samples: VecDeque<BandwidthSample>,
pub total_inbound_bytes: u64,
pub total_outbound_bytes: u64,
}Expand description
Per-peer bandwidth state keeping a sliding window of recent samples.
Fields§
§peer_id: StringStable identifier for this peer (e.g. libp2p PeerId string).
inbound_samples: VecDeque<BandwidthSample>Inbound samples within the current retention window.
outbound_samples: VecDeque<BandwidthSample>Outbound samples within the current retention window.
total_inbound_bytes: u64Running total of all inbound bytes ever recorded (never decremented).
total_outbound_bytes: u64Running total of all outbound bytes ever recorded (never decremented).
Implementations§
Source§impl PeerBandwidth
impl PeerBandwidth
Sourcepub fn new(peer_id: impl Into<String>) -> Self
pub fn new(peer_id: impl Into<String>) -> Self
Create a new, empty PeerBandwidth for the given peer.
Sourcepub fn record(&mut self, bytes: u64, direction: Direction, now: Instant)
pub fn record(&mut self, bytes: u64, direction: Direction, now: Instant)
Record a transfer of bytes in direction at time now.
Updates both the sliding-window sample queue and the all-time totals.
Sourcepub fn evict_old(&mut self, now: Instant, window: Duration)
pub fn evict_old(&mut self, now: Instant, window: Duration)
Remove samples older than window before now from both queues.
Sourcepub fn rate_bps(
&self,
direction: Direction,
now: Instant,
window: Duration,
) -> f64
pub fn rate_bps( &self, direction: Direction, now: Instant, window: Duration, ) -> f64
Compute the average rate (bytes/sec) for direction within the last
window seconds ending at now.
Returns 0.0 when there are no samples in the window or the window
duration is zero.
Sourcepub fn peak_rate_bps(&self, direction: Direction) -> f64
pub fn peak_rate_bps(&self, direction: Direction) -> f64
Compute the peak instantaneous rate (bytes/sec) for direction.
The peak is defined as the maximum single-sample byte count divided by the elapsed time since the previous sample in the queue. The very first sample is excluded because there is no prior reference point.
Returns 0.0 when fewer than two samples exist.
Sourcepub fn last_activity(&self) -> Option<Instant>
pub fn last_activity(&self) -> Option<Instant>
Return the timestamp of the most recent sample in either direction,
or None if no samples have been recorded yet.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PeerBandwidth
impl RefUnwindSafe for PeerBandwidth
impl Send for PeerBandwidth
impl Sync for PeerBandwidth
impl Unpin for PeerBandwidth
impl UnsafeUnpin for PeerBandwidth
impl UnwindSafe for PeerBandwidth
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