pub struct PeerLifetimeManager { /* private fields */ }Expand description
Manager for peer lifetime and stale peer cleanup
Tracks peer activity and determines when peers should be removed to prevent memory leaks.
Implementations§
Source§impl PeerLifetimeManager
impl PeerLifetimeManager
Sourcepub fn new(config: PeerLifetimeConfig) -> Self
pub fn new(config: PeerLifetimeConfig) -> Self
Create a new peer lifetime manager with the given configuration
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a manager with default configuration
Sourcepub fn on_peer_activity(&mut self, address: &str, connected: bool)
pub fn on_peer_activity(&mut self, address: &str, connected: bool)
Record activity for a peer
Call this when:
- A peer is discovered via advertisement
- A peer connects successfully
- Data is received from a peer
This updates the last_seen timestamp.
Sourcepub fn on_peer_disconnected(&mut self, address: &str)
pub fn on_peer_disconnected(&mut self, address: &str)
Record that a peer has disconnected
Note: This does NOT update last_seen - that’s intentional.
We want the stale timeout to start from the last actual activity,
not from the disconnect event.
Sourcepub fn is_tracked(&self, address: &str) -> bool
pub fn is_tracked(&self, address: &str) -> bool
Check if a peer is being tracked
Sourcepub fn is_connected(&self, address: &str) -> bool
pub fn is_connected(&self, address: &str) -> bool
Check if a peer is connected
Sourcepub fn get_stale_peers(&self) -> Vec<StalePeerInfo>
pub fn get_stale_peers(&self) -> Vec<StalePeerInfo>
Get the list of stale peers that should be removed
Returns addresses of peers that have exceeded their timeout:
- Disconnected peers:
disconnected_timeoutsince last seen - Connected peers:
connected_timeoutsince last seen (handles ghost connections)
Sourcepub fn get_stale_peer_addresses(&self) -> Vec<String>
pub fn get_stale_peer_addresses(&self) -> Vec<String>
Get just the addresses of stale peers
Sourcepub fn remove_peer(&mut self, address: &str) -> bool
pub fn remove_peer(&mut self, address: &str) -> bool
Remove a peer from tracking
Call this after cleaning up the peer’s resources.
Sourcepub fn cleanup_stale_peers(&mut self) -> Vec<StalePeerInfo>
pub fn cleanup_stale_peers(&mut self) -> Vec<StalePeerInfo>
Remove all stale peers and return their addresses
Convenience method that combines get_stale_peers and remove_peer.
Sourcepub fn stats(&self) -> PeerLifetimeStats
pub fn stats(&self) -> PeerLifetimeStats
Get statistics about tracked peers
Sourcepub fn get_peer_info(&self, address: &str) -> Option<PeerInfo>
pub fn get_peer_info(&self, address: &str) -> Option<PeerInfo>
Get detailed info about a specific peer
Sourcepub fn tracked_count(&self) -> usize
pub fn tracked_count(&self) -> usize
Get the number of tracked peers
Sourcepub fn cleanup_interval(&self) -> Duration
pub fn cleanup_interval(&self) -> Duration
Get the cleanup interval from configuration