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 6b7e34c commit b286fd6
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 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, NetworkBehaviour, NotifyHandler, THandler,
THandlerInEvent, THandlerOutEvent, ToSwarm,
};

use crate::peer_score::{PeerScore, PeerScoreParams, PeerScoreThresholds, RejectReason};
Expand Down Expand Up @@ -3269,6 +3269,27 @@ where
FromSwarm::ConnectionClosed(connection_closed) => {
self.on_connection_closed(connection_closed)
}
FromSwarm::DialFailure(DialFailure {
peer_id,
connection_id,
..
}) => {
if let Some(peer_id) = peer_id {

Check failure on line 3277 in protocols/gossipsub/src/behaviour.rs

View workflow job for this annotation

GitHub Actions / clippy (nightly-2023-09-10)

this `if let` can be collapsed into the outer `match`
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 b286fd6

Please sign in to comment.