Skip to main content

PeerLifetimeManager

Struct PeerLifetimeManager 

Source
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

Source

pub fn new(config: PeerLifetimeConfig) -> Self

Create a new peer lifetime manager with the given configuration

Source

pub fn with_defaults() -> Self

Create a manager with default configuration

Source

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.

Source

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.

Source

pub fn is_tracked(&self, address: &str) -> bool

Check if a peer is being tracked

Source

pub fn is_connected(&self, address: &str) -> bool

Check if a peer is connected

Source

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_timeout since last seen
  • Connected peers: connected_timeout since last seen (handles ghost connections)
Source

pub fn get_stale_peer_addresses(&self) -> Vec<String>

Get just the addresses of stale peers

Source

pub fn remove_peer(&mut self, address: &str) -> bool

Remove a peer from tracking

Call this after cleaning up the peer’s resources.

Source

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.

Source

pub fn stats(&self) -> PeerLifetimeStats

Get statistics about tracked peers

Source

pub fn get_peer_info(&self, address: &str) -> Option<PeerInfo>

Get detailed info about a specific peer

Source

pub fn clear(&mut self)

Clear all tracked peers

Source

pub fn tracked_count(&self) -> usize

Get the number of tracked peers

Source

pub fn cleanup_interval(&self) -> Duration

Get the cleanup interval from configuration

Trait Implementations§

Source§

impl Debug for PeerLifetimeManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.