Skip to content

Commit

Permalink
fix: dont remove reconnect attempts on connection closing
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Dec 20, 2024
1 parent 5e738cb commit 2016513
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/p2p/addressbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,12 @@ impl Behaviour {
list.remove(&connection_id);
if list.is_empty() && remaining_established == 0 {
entry.remove();
if let Some((duration, attempts)) = self.can_reconnect.remove(&peer_id) {
self.reconnect_peers.insert(peer_id, Delay::new(duration));
self.peer_reconnect_attempts.insert(peer_id, attempts);
if let Some((duration, attempts)) = self.can_reconnect.get(&peer_id) {
if *attempts == 0 {
return;
}
self.reconnect_peers.insert(peer_id, Delay::new(*duration));
self.peer_reconnect_attempts.insert(peer_id, 0);
}
}
}
Expand Down Expand Up @@ -282,26 +285,24 @@ impl Behaviour {
DialError::Transport(_) => {}
}

if let Some((duration, _)) = self.can_reconnect.get(&peer_id) {
if let Entry::Occupied(mut entry) = self.peer_reconnect_attempts.entry(peer_id) {
let current_attempts = entry.get_mut();
if *current_attempts == 0 {
entry.remove();
self.reconnect_peers.remove(&peer_id);
self.peer_reconnect_attempts.remove(&peer_id);
return;
}
*current_attempts -= 1;
if let Some((duration, attempts)) = self.can_reconnect.get(&peer_id) {
let current_attempts = self.peer_reconnect_attempts.entry(peer_id).or_insert(0);
if *current_attempts >= *attempts {
self.peer_reconnect_attempts.remove(&peer_id);
self.reconnect_peers.remove(&peer_id);
return;
}

if !self.reconnect_peers.contains_key(&peer_id) {
self.reconnect_peers.insert(peer_id, Delay::new(*duration));
} else {
let timer = self
.reconnect_peers
.get_mut(&peer_id)
.expect("timer available");
timer.reset(*duration);
}
*current_attempts += 1;

if !self.reconnect_peers.contains_key(&peer_id) {
self.reconnect_peers.insert(peer_id, Delay::new(*duration));
} else {
let timer = self
.reconnect_peers
.get_mut(&peer_id)
.expect("timer available");
timer.reset(*duration);
}
}
}
Expand Down

0 comments on commit 2016513

Please sign in to comment.