Skip to content

Commit

Permalink
Attempt to eliminate stale peers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revertron committed May 27, 2023
1 parent e2f0fdf commit 0930314
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/p2p/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,18 @@ impl Peers {
registry.reregister(stream, *token, Interest::WRITABLE | Interest::READABLE).unwrap();
}
} else {
if !matches!(peer.get_state(), State::Connecting {..}) && (peer.get_state().is_timed_out() || !peer.active()) {
stale_tokens.push((token.clone(), peer.get_addr()));
continue;
}
if matches!(peer.get_state(), State::Message {..}) {
if !peer.active() {
stale_tokens.push((token.clone(), peer.get_addr()));
continue;
}
let stream = peer.get_stream();
registry.reregister(stream, *token, Interest::WRITABLE).unwrap();
}
}
}
for (token, addr) in &stale_tokens {
info!("Closing stale peer from {}", addr);
info!("Closing stale (timed out) peer from {}", addr);
self.close_peer(registry, token);
}

Expand Down
11 changes: 11 additions & 0 deletions src/p2p/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ impl State {
matches!(self, State::Idle { .. })
}

pub fn is_timed_out(&self) -> bool {
match self {
State::Error => true,
State::Banned => true,
State::Idle { from } => {
from.elapsed().as_secs() > 60
}
_ => false
}
}

pub fn is_loop(&self) -> bool {
matches!(self, State::Loop { .. } | State::SendLoop { .. })
}
Expand Down

0 comments on commit 0930314

Please sign in to comment.