pub struct PeerExchangeProtocol { /* private fields */ }Expand description
Peer Exchange Protocol implementation.
Manages a set of known peers and supports periodic exchange of peer lists with connected peers for decentralised peer discovery.
Implementations§
Source§impl PeerExchangeProtocol
impl PeerExchangeProtocol
Sourcepub fn new(config: PexConfig) -> Self
pub fn new(config: PexConfig) -> Self
Create a new PeerExchangeProtocol with the given configuration.
Sourcepub fn add_peer(
&mut self,
peer_id: &str,
addresses: Vec<String>,
source: PeerSource,
)
pub fn add_peer( &mut self, peer_id: &str, addresses: Vec<String>, source: PeerSource, )
Add or update a known peer.
If the peer already exists, its addresses and last-seen tick are updated. The source is upgraded if the new source has higher priority (Direct > Exchange > Bootstrap).
Sourcepub fn remove_peer(&mut self, peer_id: &str) -> bool
pub fn remove_peer(&mut self, peer_id: &str) -> bool
Remove a peer from the known peer set.
Returns true if the peer existed and was removed.
Sourcepub fn select_for_exchange(&self) -> Vec<&PexPeerRecord>
pub fn select_for_exchange(&self) -> Vec<&PexPeerRecord>
Select peers for exchange.
Returns up to max_peers_per_exchange peers, preferring
Direct > Exchange > Bootstrap, and within each source category
preferring the most recently seen peers.
Sourcepub fn receive_exchange(&mut self, peers: Vec<(String, Vec<String>)>)
pub fn receive_exchange(&mut self, peers: Vec<(String, Vec<String>)>)
Receive a set of peers from a peer exchange.
All received peers are added with PeerSource::Exchange. If the
total number of known peers exceeds max_known_peers, the oldest
peers (by last_seen_tick) are evicted.
Sourcepub fn should_exchange(&self) -> bool
pub fn should_exchange(&self) -> bool
Returns true if enough ticks have elapsed since the last
exchange to warrant a new one.
Sourcepub fn get_peer(&self, peer_id: &str) -> Option<&PexPeerRecord>
pub fn get_peer(&self, peer_id: &str) -> Option<&PexPeerRecord>
Look up a peer by ID.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Return the number of known peers.
Sourcepub fn peers_by_source(&self, source: PeerSource) -> Vec<&PexPeerRecord>
pub fn peers_by_source(&self, source: PeerSource) -> Vec<&PexPeerRecord>
Return all peers discovered via the given source.
Auto Trait Implementations§
impl Freeze for PeerExchangeProtocol
impl RefUnwindSafe for PeerExchangeProtocol
impl Send for PeerExchangeProtocol
impl Sync for PeerExchangeProtocol
impl Unpin for PeerExchangeProtocol
impl UnsafeUnpin for PeerExchangeProtocol
impl UnwindSafe for PeerExchangeProtocol
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