Skip to content

Commit

Permalink
fix: peer connection to stale nodes (tari-project#5579)
Browse files Browse the repository at this point in the history
Description
---
This fixes nodes continuously selecting bad stale nodes as connections
and attempting to connect to them.

Motivation and Context
---
If nodes have bad nodes in their peer database they should not keep
trying to connect to them.
This will attempt to connect to a node once, and if the node was never
online and it tried to connect to the node once, it will ignore the node
till the node tries to connect to it first.

This stops the problem with if a node has too many stale connections in
its peer database, it will keep selecting stale nodes and try to connect
to them. If the amount of stale nodes are too much, it can cause a
constant read/write on the peer database of the node.

How Has This Been Tested?
---
Manually verified with database with mostly stale nodes. Node will try
them once, then ignore them.
  • Loading branch information
SWvheerden authored Aug 8, 2023
1 parent b8196ac commit eebda00
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions comms/core/src/peer_manager/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,23 @@ impl Peer {
&self.supported_protocols
}

pub fn last_connect_attempt(&self) -> Option<NaiveDateTime> {
let mut last_connected_attempt = None;
for address in self.addresses.addresses() {
if let Some(address_time) = address.last_attempted {
match last_connected_attempt {
Some(last_time) => {
if last_time < address_time {
last_connected_attempt = address.last_attempted
}
},
None => last_connected_attempt = address.last_attempted,
}
}
}
last_connected_attempt
}

/// Returns true if the peer is marked as offline
pub fn is_offline(&self) -> bool {
self.addresses.offline_at().is_some()
Expand Down
4 changes: 4 additions & 0 deletions comms/dht/src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,10 @@ impl DhtConnectivity {
{
return false;
}
// we have tried to connect to this peer, and we have never made a successful attempt at connection
if peer.offline_since().is_none() && peer.last_connect_attempt().is_some() {
return false;
}

let is_excluded = excluded.contains(&peer.node_id);
if is_excluded {
Expand Down

0 comments on commit eebda00

Please sign in to comment.