pub struct PeerLatencyPredictor {
pub states: HashMap<String, PeerLatencyState>,
pub config: PredictorConfig,
pub total_samples: u64,
}Expand description
Tracks per-peer RTT samples and provides latency predictions.
Fields§
§states: HashMap<String, PeerLatencyState>Per-peer state keyed by peer ID string.
config: PredictorConfigPredictor configuration.
total_samples: u64Cumulative sample count across all peers.
Implementations§
Source§impl PeerLatencyPredictor
impl PeerLatencyPredictor
Sourcepub fn new(config: PredictorConfig) -> Self
pub fn new(config: PredictorConfig) -> Self
Creates a new predictor with the given configuration.
Sourcepub fn record_sample(&mut self, sample: LatencySample)
pub fn record_sample(&mut self, sample: LatencySample)
Records a latency sample for a peer, updating EWMA and variance.
On the first sample for a peer the EWMA is initialised to the
observed RTT and the variance to 0.0. Subsequent samples update via:
ewma = alpha * rtt + (1 - alpha) * ewma
deviation = rtt - ewma (post-update deviation)
variance = beta * deviation^2 + (1 - beta) * varianceSourcepub fn predict(&self, peer_id: &str) -> Option<f64>
pub fn predict(&self, peer_id: &str) -> Option<f64>
Returns the predicted RTT for peer_id, or None if unknown.
Sourcepub fn evict_stale(&mut self, now_secs: u64) -> usize
pub fn evict_stale(&mut self, now_secs: u64) -> usize
Removes peers whose last sample is older than now_secs - stale_threshold_secs.
Returns the number of peers evicted.
Sourcepub fn best_peers(&self, n: usize) -> Vec<&str>
pub fn best_peers(&self, n: usize) -> Vec<&str>
Returns peer IDs of the n peers with the lowest predicted RTT, in
ascending order (best first).
If n exceeds the number of tracked peers, all are returned.
Sourcepub fn stats(&self) -> PredictorStats
pub fn stats(&self) -> PredictorStats
Returns a snapshot of predictor statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PeerLatencyPredictor
impl RefUnwindSafe for PeerLatencyPredictor
impl Send for PeerLatencyPredictor
impl Sync for PeerLatencyPredictor
impl Unpin for PeerLatencyPredictor
impl UnsafeUnpin for PeerLatencyPredictor
impl UnwindSafe for PeerLatencyPredictor
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