From 7329d2cb370883e06f6772c0bbe855069d051573 Mon Sep 17 00:00:00 2001 From: Yiannis Marangos Date: Mon, 10 Jun 2024 13:14:12 +0300 Subject: [PATCH] feat(node): Close connections that failed on ping --- node/src/p2p.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/node/src/p2p.rs b/node/src/p2p.rs index d7106a95..85a69269 100644 --- a/node/src/p2p.rs +++ b/node/src/p2p.rs @@ -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, @@ -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) {