pub struct PeerDiscoveryProtocol { /* private fields */ }Expand description
Multi-strategy peer discovery protocol.
Maintains a bounded peer table with TTL expiry, verification tracking,
capability filtering, and per-method statistics. All state transitions emit
PdpDiscoveryEvent values buffered internally and drained by the caller.
Implementations§
Source§impl PeerDiscoveryProtocol
impl PeerDiscoveryProtocol
Sourcepub fn new(config: PdpDiscoveryConfig) -> Result<Self, PdpDiscoveryError>
pub fn new(config: PdpDiscoveryConfig) -> Result<Self, PdpDiscoveryError>
Create a new protocol instance with the given configuration.
§Errors
Returns PdpDiscoveryError::ConfigurationError if max_peers is 0
or peer_ttl_us is 0.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a new protocol instance with the default configuration.
Sourcepub fn add_peer(
&mut self,
peer: PdpDiscoveredPeer,
) -> Result<PdpDiscoveryEvent, PdpDiscoveryError>
pub fn add_peer( &mut self, peer: PdpDiscoveredPeer, ) -> Result<PdpDiscoveryEvent, PdpDiscoveryError>
Add a peer to the table.
- If the peer is new and the table has capacity, it is inserted and
PdpDiscoveryEvent::PeerDiscoveredis emitted. - If the peer already exists, its addresses are merged (deduped) and
its TTL is refreshed;
PdpDiscoveryEvent::PeerDiscoveredis emitted again to signal the update. - If the table is full and the peer is unknown,
PdpDiscoveryError::MaxPeersExceededis returned andPdpDiscoveryEvent::PeerTableFullis emitted.
Sourcepub fn remove_peer(&mut self, id: &str) -> Result<(), PdpDiscoveryError>
pub fn remove_peer(&mut self, id: &str) -> Result<(), PdpDiscoveryError>
Sourcepub fn verify_peer(
&mut self,
id: &str,
current_ts: u64,
) -> Result<PdpDiscoveryEvent, PdpDiscoveryError>
pub fn verify_peer( &mut self, id: &str, current_ts: u64, ) -> Result<PdpDiscoveryEvent, PdpDiscoveryError>
Mark a peer as verified and record the current timestamp.
Emits PdpDiscoveryEvent::PeerVerified on success.
§Errors
Returns PdpDiscoveryError::PeerNotFound if the peer does not exist.
Sourcepub fn expire_peers(&mut self, current_ts: u64) -> Vec<PdpDiscoveryEvent>
pub fn expire_peers(&mut self, current_ts: u64) -> Vec<PdpDiscoveryEvent>
Remove all peers whose TTL has elapsed relative to current_ts.
Returns one PdpDiscoveryEvent::PeerExpired per removed peer.
Sourcepub fn refresh_peer(
&mut self,
id: &str,
current_ts: u64,
) -> Result<(), PdpDiscoveryError>
pub fn refresh_peer( &mut self, id: &str, current_ts: u64, ) -> Result<(), PdpDiscoveryError>
Reset the TTL of a known peer to current_ts + config.peer_ttl_us.
§Errors
Returns PdpDiscoveryError::PeerNotFound if the peer does not exist.
Sourcepub fn get_peer(
&self,
id: &str,
) -> Result<&PdpDiscoveredPeer, PdpDiscoveryError>
pub fn get_peer( &self, id: &str, ) -> Result<&PdpDiscoveredPeer, PdpDiscoveryError>
Retrieve an immutable reference to a peer by id.
§Errors
Returns PdpDiscoveryError::PeerNotFound if the peer does not exist.
Sourcepub fn peers_by_method(&self, method: &str) -> Vec<&PdpDiscoveredPeer>
pub fn peers_by_method(&self, method: &str) -> Vec<&PdpDiscoveredPeer>
Return all peers discovered by the given method variant name.
The method string must match one of the values returned by
PdpDiscoveryMethod::variant_name (e.g., "bootstrap", "mdns").
Sourcepub fn peers_with_capability(&self, capability: &str) -> Vec<&PdpDiscoveredPeer>
pub fn peers_with_capability(&self, capability: &str) -> Vec<&PdpDiscoveredPeer>
Return all peers that have the given capability string.
Sourcepub fn random_peers(&self, n: usize, seed: u64) -> Vec<&PdpDiscoveredPeer>
pub fn random_peers(&self, n: usize, seed: u64) -> Vec<&PdpDiscoveredPeer>
Return up to n randomly selected peers using an xorshift64-based
Fisher-Yates shuffle seeded with seed.
If the table has fewer than n peers, all peers are returned in
shuffled order.
Sourcepub fn merge_peer_table(
&mut self,
other: Vec<PdpDiscoveredPeer>,
current_ts: u64,
) -> Vec<PdpDiscoveryEvent>
pub fn merge_peer_table( &mut self, other: Vec<PdpDiscoveredPeer>, current_ts: u64, ) -> Vec<PdpDiscoveryEvent>
Bulk-import peers from a PeerExchange handshake.
For each peer in other:
- Skip if already expired (
discovered_at + ttl_us < current_ts). - If the peer is already known, merge addresses + capabilities and refresh TTL.
- If the peer is new, insert it (subject to capacity).
Returns the list of events generated.
Sourcepub fn stats(&self) -> PdpDiscoveryStats
pub fn stats(&self) -> PdpDiscoveryStats
Return a snapshot of current statistics.
Sourcepub fn drain_events(&mut self) -> Vec<PdpDiscoveryEvent>
pub fn drain_events(&mut self) -> Vec<PdpDiscoveryEvent>
Drain and return all buffered events.
After this call the internal event buffer is empty.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Return the current number of peers in the table.
Sourcepub fn all_peers(&self) -> impl Iterator<Item = &PdpDiscoveredPeer>
pub fn all_peers(&self) -> impl Iterator<Item = &PdpDiscoveredPeer>
Return an iterator over all peers currently in the table.
Sourcepub fn record_method_failure(
&mut self,
method: impl Into<String>,
reason: impl Into<String>,
)
pub fn record_method_failure( &mut self, method: impl Into<String>, reason: impl Into<String>, )
Record a discovery method failure, emitting
PdpDiscoveryEvent::DiscoveryMethodFailed.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PeerDiscoveryProtocol
impl RefUnwindSafe for PeerDiscoveryProtocol
impl Send for PeerDiscoveryProtocol
impl Sync for PeerDiscoveryProtocol
impl Unpin for PeerDiscoveryProtocol
impl UnsafeUnpin for PeerDiscoveryProtocol
impl UnwindSafe for PeerDiscoveryProtocol
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