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 baa38e9
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 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,32 @@ 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 {
let mut peer = self
.connected_peers
.remove(&peer_id)
.expect("To be connected to peer");
let index = peer
.connections
.iter()
.position(|v| v == &connection_id)
.expect("Previously established connection to peer must be present");
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 baa38e9

Please sign in to comment.