pub struct PeerTracker { /* private fields */ }Expand description
The client’s local view of the known peer set and the entry point for Kademlia-style routing.
PeerTracker owns the in-memory list of Peer records the client has seen, persists
them to ClientStorage under BUCKET_PEER so they survive restarts, and exposes the
iteration primitives used throughout the client when it needs to answer “who should I
talk to next about this Id?”. When the list is empty (first launch, or after a
reset), it seeds itself via a BootstrapV1 RPC against the
crate::transport::bootstrap_provider::bootstrap_provider::BootstrapProvider addresses configured on the
transport.
The tracker is the single source of truth for peer freshness: stale or bad peers get
evicted here, new peers get folded in here, and the peers_need_flush flag coalesces
rapid updates into a single disk write.
Implementations§
Source§impl PeerTracker
impl PeerTracker
pub async fn new( runtime_services: Arc<RuntimeServices>, client_storage: Arc<dyn ClientStorage>, ) -> Result<Self>
pub async fn new_with_max_peers( runtime_services: Arc<RuntimeServices>, client_storage: Arc<dyn ClientStorage>, max_peers: usize, ) -> Result<Self>
Sourcepub fn take_flush_snapshot(&mut self) -> Option<Vec<Peer>>
pub fn take_flush_snapshot(&mut self) -> Option<Vec<Peer>>
Clears the dirty flag and returns the peers to persist, or None if nothing changed since the last flush.
pub fn mark_needs_flush(&mut self)
pub fn current_time_millis(&self) -> TimeMillis
Sourcepub fn add_peer(&mut self, peer: Peer) -> Result<()>
pub fn add_peer(&mut self, peer: Peer) -> Result<()>
Verify-and-insert in one go. Holders of a SharedPeerTracker should instead call Self::needs_verification under a read lock,
Self::verify_candidate with no lock held, then Self::insert_verified under a write lock - verification is the expensive part.
Sourcepub fn needs_verification(&self, peer: &Peer) -> bool
pub fn needs_verification(&self, peer: &Peer) -> bool
Cheap dedupe before the expensive verify: servers resend the same peers in every peers_nearer list. False if we already hold this peer with a timestamp at least as new.
Sourcepub fn verify_candidate(
peer: &Peer,
current_time_millis: TimeMillis,
) -> Result<()>
pub fn verify_candidate( peer: &Peer, current_time_millis: TimeMillis, ) -> Result<()>
Everything a peer must pass before it may enter the tracker. Pure, so it can run with no lock held.
Sourcepub fn insert_verified(&mut self, peer: Peer)
pub fn insert_verified(&mut self, peer: Peer)
Inserts a peer that has passed Self::verify_candidate. Re-checks the timestamp dedupe, because another walk may have
inserted a newer copy between our verify and this insert.
pub fn remove_peer(&mut self, peer: &Peer)
pub fn is_empty(&self) -> bool
pub fn needs_flush(&self) -> bool
pub fn len(&self) -> usize
pub fn peers(&self) -> &Vec<Peer>
Sourcepub async fn flush(peer_tracker: &SharedPeerTracker) -> Result<()>
pub async fn flush(peer_tracker: &SharedPeerTracker) -> Result<()>
Persists the peers if they changed. The storage write happens with no tracker lock held.
Sourcepub async fn iterate_to_location(
peer_tracker: &SharedPeerTracker,
bucket_location_id: Id,
max_iterations_since_high_watermark: usize,
cache_radius: Option<LeadingAgreementBits>,
) -> Result<PeerIterator>
pub async fn iterate_to_location( peer_tracker: &SharedPeerTracker, bucket_location_id: Id, max_iterations_since_high_watermark: usize, cache_radius: Option<LeadingAgreementBits>, ) -> Result<PeerIterator>
Bootstraps if necessary, then starts a walk towards bucket_location_id. The iterator locks the tracker only briefly per step.
Sourcepub async fn bootstrap(peer_tracker: &SharedPeerTracker) -> Result<()>
pub async fn bootstrap(peer_tracker: &SharedPeerTracker) -> Result<()>
Seeds an empty tracker from the bootstrap addresses. RPCs and verification happen with no lock held; two concurrent bootstraps may both fetch, which is harmless because inserts are idempotent.
Auto Trait Implementations§
impl Freeze for PeerTracker
impl !RefUnwindSafe for PeerTracker
impl Send for PeerTracker
impl Sync for PeerTracker
impl Unpin for PeerTracker
impl UnsafeUnpin for PeerTracker
impl !UnwindSafe for PeerTracker
Blanket Implementations§
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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