Skip to content

Commit

Permalink
remove peer from connected_peers if connection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Jan 8, 2024
1 parent d355e42 commit b64bb06
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use libp2p_identity::PeerId;
use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm},
dial_opts::DialOpts,
ConnectionDenied, ConnectionId, NetworkBehaviour, NotifyHandler, THandler, THandlerInEvent,
THandlerOutEvent, ToSwarm,
ConnectionDenied, ConnectionId, DialFailure, ListenFailure, NetworkBehaviour, NotifyHandler,
THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};

use crate::peer_score::{PeerScore, PeerScoreParams, PeerScoreThresholds, RejectReason};
Expand Down Expand Up @@ -3269,6 +3269,30 @@ where
FromSwarm::ConnectionClosed(connection_closed) => {
self.on_connection_closed(connection_closed)
}
FromSwarm::DialFailure(DialFailure {
peer_id,
connection_id,
..
})
| FromSwarm::ListenFailure(ListenFailure {
connection_id,
peer_id,
..
}) => {
if let Some(peer_id) = peer_id {
if let Some(mut peer) = self.connected_peers.remove(&peer_id) {
if let Some(index) =
peer.connections.iter().position(|v| v == &connection_id)
{
peer.connections.remove(index);
}

if !peer.connections.is_empty() {
self.connected_peers.insert(peer_id, peer);
}
}
}
}
FromSwarm::AddressChange(address_change) => self.on_address_change(address_change),
_ => {}
}
Expand Down

0 comments on commit b64bb06

Please sign in to comment.