Skip to content

Commit

Permalink
take last seen into account - prevent mass delete if tor shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 22, 2021
1 parent 0cbe04f commit 45a0cb4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions comms/src/connectivity/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub struct ConnectivityConfig {
/// The length of time to wait before disconnecting a connection that failed tie breaking.
/// Default: 1s
pub connection_tie_break_linger: Duration,
/// If the peer has not been seen within this interval, it will be removed from the peer list on the
/// next connection attempt.
/// Default: 24 hours
pub expire_peer_last_seen_duration: Duration,
}

impl Default for ConnectivityConfig {
Expand All @@ -52,6 +56,7 @@ impl Default for ConnectivityConfig {
is_connection_reaping_enabled: true,
max_failures_mark_offline: 2,
connection_tie_break_linger: Duration::from_secs(2),
expire_peer_last_seen_duration: Duration::from_secs(24 * 60 * 60),
}
}
}
11 changes: 10 additions & 1 deletion comms/src/connectivity/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,17 @@ impl ConnectivityManagerActor {
target: LOG_TARGET,
"Peer `{}` was marked as offline after {} attempts. Removing peer from peer list", num_failed, node_id
);
self.peer_manager.delete_peer(node_id).await?;

self.connection_stats.remove(node_id);
if let Ok(peer) = self.peer_manager.find_by_node_id(node_id).await {
if peer
.last_seen_since()
.map(|t| t > self.config.expire_peer_last_seen_duration)
.unwrap_or(true)
{
self.peer_manager.delete_peer(node_id).await?;
}
}
}

Ok(())
Expand Down
6 changes: 6 additions & 0 deletions comms/src/peer_manager/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ impl Peer {
self.addresses.last_seen()
}

/// Provides that length of time since the last successful interaction with the peer
pub fn last_seen_since(&self) -> Option<Duration> {
self.last_seen()
.and_then(|dt| Utc::now().signed_duration_since(dt).to_std().ok())
}

/// Returns true if this peer has the given feature, otherwise false
pub fn has_features(&self, features: PeerFeatures) -> bool {
self.features.contains(features)
Expand Down

0 comments on commit 45a0cb4

Please sign in to comment.