Skip to content

Commit

Permalink
feat(node): Close connections that failed on ping
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Jun 10, 2024
1 parent 8094d04 commit 7329d2c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions node/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,8 @@ where
BehaviourEvent::Gossipsub(ev) => self.on_gossip_sub_event(ev).await,
BehaviourEvent::Kademlia(ev) => self.on_kademlia_event(ev).await?,
BehaviourEvent::Bitswap(ev) => self.on_bitswap_event(ev).await,
BehaviourEvent::Autonat(_)
| BehaviourEvent::Ping(_)
| BehaviourEvent::HeaderEx(_) => {}
BehaviourEvent::Ping(ev) => self.on_ping_event(ev).await,
BehaviourEvent::Autonat(_) | BehaviourEvent::HeaderEx(_) => {}
},
SwarmEvent::ConnectionEstablished {
peer_id,
Expand Down Expand Up @@ -861,6 +860,23 @@ where
}
}

#[instrument(level = "debug", skip_all)]
async fn on_ping_event(&mut self, ev: ping::Event) {
match ev.result {
Ok(dur) => debug!(
"Ping success: peer: {}, connection_id: {}, time: {:?}",
ev.peer, ev.connection, dur
),
Err(e) => {
debug!(
"Ping failure: peer: {}, connection_id: {}, error: {}",
&ev.peer, &ev.connection, e
);
self.swarm.close_connection(ev.connection);
}
}
}

#[instrument(skip_all, fields(peer_id = %peer_id))]
fn peer_maybe_discovered(&mut self, peer_id: PeerId) {
if !self.peer_tracker.set_maybe_discovered(peer_id) {
Expand Down

0 comments on commit 7329d2c

Please sign in to comment.