pub struct PeerScoringSystem {
pub weights: ScoringWeights,
pub history: HashMap<String, VecDeque<PsPeerScore>>,
pub max_history: usize,
}Expand description
Composite peer quality scoring engine.
Maintains a rolling score history per peer and exposes scoring, ranking, trend analysis, and stale-peer eviction.
Fields§
§weights: ScoringWeightsDimension weights used when computing composite scores.
history: HashMap<String, VecDeque<PsPeerScore>>Rolling score history per peer, keyed by peer ID.
max_history: usizeMaximum number of recent scores retained per peer.
Implementations§
Source§impl PeerScoringSystem
impl PeerScoringSystem
Sourcepub fn new(weights: ScoringWeights) -> Result<Self, ScoringError>
pub fn new(weights: ScoringWeights) -> Result<Self, ScoringError>
Construct a new system with the given scoring weights.
Returns ScoringError::WeightsMustSumToOne if the weights do not
sum to approximately 1.0.
Sourcepub fn with_max_history(self, max_history: usize) -> Self
pub fn with_max_history(self, max_history: usize) -> Self
Construct with a custom max_history capacity.
Sourcepub fn score_peer(&self, metrics: &PeerMetrics, now: u64) -> PsPeerScore
pub fn score_peer(&self, metrics: &PeerMetrics, now: u64) -> PsPeerScore
Compute a PsPeerScore from raw PeerMetrics without storing it.
§Component formulas (all output in [0, 1]):
- Latency :
exp(-rtt_ms / 500.0)— 0 ms → 1.0, 500 ms → 0.368, 1 000 ms → 0.135 - Bandwidth :
(bandwidth_bps / 1_000_000).min(1.0)— caps at 1 Mbps - Availability :
uptime_fraction.clamp(0, 1) - Reliability :
success_rate.clamp(0, 1) - GossipQuality :
gossip_delivery_rate.clamp(0, 1) - RoutingQuality :
routing_success_rate.clamp(0, 1)
Sourcepub fn score_and_record(
&mut self,
metrics: &PeerMetrics,
now: u64,
) -> PsPeerScore
pub fn score_and_record( &mut self, metrics: &PeerMetrics, now: u64, ) -> PsPeerScore
Compute a score and push it into the peer’s history ring-buffer.
If the buffer is at max_history capacity, the oldest entry is evicted.
Sourcepub fn history_for(&self, peer_id: &str) -> &[PsPeerScore]
pub fn history_for(&self, peer_id: &str) -> &[PsPeerScore]
Return the history slice for peer_id (oldest first, newest last).
Returns an empty slice if the peer has no recorded history.
Sourcepub fn history_vec(&self, peer_id: &str) -> Vec<&PsPeerScore>
pub fn history_vec(&self, peer_id: &str) -> Vec<&PsPeerScore>
Return a Vec of all history entries for peer_id in order
(oldest first, newest last).
Unlike history_for, this always returns the
full sequence even when the internal deque is split across two slices.
Sourcepub fn average_score(&self, peer_id: &str) -> Option<f64>
pub fn average_score(&self, peer_id: &str) -> Option<f64>
Return the mean composite score for peer_id across all recorded
history entries, or None if there is no history.
Sourcepub fn trend(&self, peer_id: &str) -> Option<f64>
pub fn trend(&self, peer_id: &str) -> Option<f64>
Compute the slope of a linear regression over the composite scores in the peer’s history (positive slope = improving quality over time).
Returns None if fewer than 2 data points are available.
The independent variable is the index in the history sequence (0, 1, 2, …), so the slope represents change per observation step.
Sourcepub fn top_peers(&self, n: usize, _now: u64) -> Vec<&PsPeerScore>
pub fn top_peers(&self, n: usize, _now: u64) -> Vec<&PsPeerScore>
Return references to the top n peers ordered by their most-recent
composite score (descending).
Peers with no history are excluded.
Sourcepub fn evict_stale(&mut self, max_age_ms: u64, now: u64) -> usize
pub fn evict_stale(&mut self, max_age_ms: u64, now: u64) -> usize
Remove peers whose latest recorded score is older than max_age_ms
milliseconds relative to now.
Returns the number of peers evicted.
Sourcepub fn stats(&self, max_age_ms: u64, now: u64) -> ScoringStats
pub fn stats(&self, max_age_ms: u64, now: u64) -> ScoringStats
Compute aggregate statistics for the scoring system.
max_age_ms and now are used solely to determine which peers are
considered “stale” for the ScoringStats::stale_peers field;
stale peers are not evicted by this call.
Trait Implementations§
Source§impl Debug for PeerScoringSystem
impl Debug for PeerScoringSystem
Auto Trait Implementations§
impl Freeze for PeerScoringSystem
impl RefUnwindSafe for PeerScoringSystem
impl Send for PeerScoringSystem
impl Sync for PeerScoringSystem
impl Unpin for PeerScoringSystem
impl UnsafeUnpin for PeerScoringSystem
impl UnwindSafe for PeerScoringSystem
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