pub struct PeerDiscoveryCache {
pub records: HashMap<String, PeerRecord>,
pub max_size: usize,
pub ttl_secs: u64,
}Expand description
Cache for peer discovery results with TTL and capacity management.
Fields§
§records: HashMap<String, PeerRecord>Peer records keyed by peer_id
max_size: usizeMaximum number of records to retain
ttl_secs: u64Time-to-live in seconds; records older than this are stale
Implementations§
Source§impl PeerDiscoveryCache
impl PeerDiscoveryCache
Sourcepub fn upsert(&mut self, record: PeerRecord)
pub fn upsert(&mut self, record: PeerRecord)
Insert or update a peer record.
If the peer already exists, addresses are merged (deduped), last_seen_secs is
updated if the incoming record is newer, and source is updated.
If the cache is at capacity and the peer is new, the peer with the lowest
reliability_score is evicted (ties broken by oldest last_seen_secs).
Sourcepub fn record_success(&mut self, peer_id: &str, now_secs: u64) -> bool
pub fn record_success(&mut self, peer_id: &str, now_secs: u64) -> bool
Record a successful connection to a peer.
Increments successful_connections and updates last_seen_secs.
Returns false if the peer is not in the cache.
Sourcepub fn record_failure(&mut self, peer_id: &str) -> bool
pub fn record_failure(&mut self, peer_id: &str) -> bool
Record a failed connection attempt to a peer.
Increments failed_connections.
Returns false if the peer is not in the cache.
Sourcepub fn evict_stale(&mut self, now_secs: u64)
pub fn evict_stale(&mut self, now_secs: u64)
Remove all records where last_seen_secs + ttl_secs < now_secs.
Sourcepub fn get(&self, peer_id: &str) -> Option<&PeerRecord>
pub fn get(&self, peer_id: &str) -> Option<&PeerRecord>
Look up a peer record by peer_id.
Sourcepub fn peers_by_reliability(&self) -> Vec<&PeerRecord>
pub fn peers_by_reliability(&self) -> Vec<&PeerRecord>
Return all peer records sorted by reliability score descending.
Sourcepub fn peers_from_source(&self, source: DiscoverySource) -> Vec<&PeerRecord>
pub fn peers_from_source(&self, source: DiscoverySource) -> Vec<&PeerRecord>
Return all peers discovered from a specific source, sorted by last_seen_secs descending.
Sourcepub fn remove(&mut self, peer_id: &str) -> bool
pub fn remove(&mut self, peer_id: &str) -> bool
Remove a peer from the cache.
Returns true if the peer was present and removed, false otherwise.
Sourcepub fn stats(&self) -> DiscoveryCacheStats
pub fn stats(&self) -> DiscoveryCacheStats
Compute and return aggregate cache statistics.
Auto Trait Implementations§
impl Freeze for PeerDiscoveryCache
impl RefUnwindSafe for PeerDiscoveryCache
impl Send for PeerDiscoveryCache
impl Sync for PeerDiscoveryCache
impl Unpin for PeerDiscoveryCache
impl UnsafeUnpin for PeerDiscoveryCache
impl UnwindSafe for PeerDiscoveryCache
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