pub struct BootstrapCache { /* private fields */ }Expand description
Greedy bootstrap cache with quality-based peer selection.
This cache stores peer information with quality metrics and provides epsilon-greedy selection to balance exploitation (using known-good peers) with exploration (trying new peers to discover potentially better ones).
Implementations§
Source§impl BootstrapCache
impl BootstrapCache
Sourcepub async fn open(config: BootstrapCacheConfig) -> Result<Self>
pub async fn open(config: BootstrapCacheConfig) -> Result<Self>
Open or create a bootstrap cache.
Loads existing cache data from disk if available, otherwise starts fresh.
Sourcepub fn subscribe(&self) -> Receiver<CacheEvent>
pub fn subscribe(&self) -> Receiver<CacheEvent>
Subscribe to cache events
Sourcepub async fn peer_count(&self) -> usize
pub async fn peer_count(&self) -> usize
Get the number of cached peers
Sourcepub async fn select_peers(&self, count: usize) -> Vec<CachedPeer>
pub async fn select_peers(&self, count: usize) -> Vec<CachedPeer>
Select peers for bootstrap using epsilon-greedy strategy.
Returns up to count peers, balancing exploitation of known-good peers
with exploration of untested peers based on the configured epsilon.
Sourcepub async fn select_relay_peers(&self, count: usize) -> Vec<CachedPeer>
pub async fn select_relay_peers(&self, count: usize) -> Vec<CachedPeer>
Select peers that support relay functionality.
Returns peers sorted by quality score that have relay capability.
Sourcepub async fn select_coordinators(&self, count: usize) -> Vec<CachedPeer>
pub async fn select_coordinators(&self, count: usize) -> Vec<CachedPeer>
Select peers that support NAT coordination.
Returns peers sorted by quality score that have coordination capability.
Sourcepub async fn upsert(&self, peer: CachedPeer)
pub async fn upsert(&self, peer: CachedPeer)
Add or update a peer in the cache.
If the cache is at capacity, evicts the lowest quality peers.
Sourcepub async fn add_seed(&self, peer_id: PeerId, addresses: Vec<SocketAddr>)
pub async fn add_seed(&self, peer_id: PeerId, addresses: Vec<SocketAddr>)
Add a seed peer (user-provided bootstrap node).
Sourcepub async fn add_from_connection(
&self,
peer_id: PeerId,
addresses: Vec<SocketAddr>,
caps: Option<PeerCapabilities>,
)
pub async fn add_from_connection( &self, peer_id: PeerId, addresses: Vec<SocketAddr>, caps: Option<PeerCapabilities>, )
Add a peer discovered from an active connection.
Sourcepub async fn record_outcome(&self, peer_id: &PeerId, outcome: ConnectionOutcome)
pub async fn record_outcome(&self, peer_id: &PeerId, outcome: ConnectionOutcome)
Record a connection attempt result.
Sourcepub async fn record_success(&self, peer_id: &PeerId, rtt_ms: u32)
pub async fn record_success(&self, peer_id: &PeerId, rtt_ms: u32)
Record successful connection.
Sourcepub async fn record_failure(&self, peer_id: &PeerId)
pub async fn record_failure(&self, peer_id: &PeerId)
Record failed connection.
Sourcepub async fn update_capabilities(
&self,
peer_id: &PeerId,
caps: PeerCapabilities,
)
pub async fn update_capabilities( &self, peer_id: &PeerId, caps: PeerCapabilities, )
Update peer capabilities.
Sourcepub async fn get(&self, peer_id: &PeerId) -> Option<CachedPeer>
pub async fn get(&self, peer_id: &PeerId) -> Option<CachedPeer>
Get a specific peer.
Sourcepub async fn remove(&self, peer_id: &PeerId) -> Option<CachedPeer>
pub async fn remove(&self, peer_id: &PeerId) -> Option<CachedPeer>
Remove a peer from cache.
Sourcepub async fn cleanup_stale(&self) -> usize
pub async fn cleanup_stale(&self) -> usize
Cleanup stale peers.
Removes peers that haven’t been seen within the stale threshold. Returns the number of peers removed.
Sourcepub async fn recalculate_quality(&self)
pub async fn recalculate_quality(&self)
Recalculate quality scores for all peers.
Sourcepub async fn stats(&self) -> CacheStats
pub async fn stats(&self) -> CacheStats
Get cache statistics.
Sourcepub fn start_maintenance(self: Arc<Self>) -> JoinHandle<()>
pub fn start_maintenance(self: Arc<Self>) -> JoinHandle<()>
Start background maintenance tasks.
Spawns a task that periodically:
- Saves the cache to disk
- Cleans up stale peers
- Recalculates quality scores
Returns a handle that can be used to cancel the task.
Sourcepub async fn all_peers(&self) -> Vec<CachedPeer>
pub async fn all_peers(&self) -> Vec<CachedPeer>
Get all cached peers (for export/debug).
Sourcepub fn config(&self) -> &BootstrapCacheConfig
pub fn config(&self) -> &BootstrapCacheConfig
Get the configuration.