mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-24 15:21:33 +00:00
protocols/relay/src/v2: Remove empty peer entries in self.reservations (#2464)
When a peer disconnects, reservations associated with that peer are removed from the set of reservations of the peer. In case the set of reservations for the peer is now empty, remove the entire peer. Same when a reservation times out.
This commit is contained in:
@ -35,7 +35,7 @@ use libp2p_swarm::{
|
||||
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
};
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
|
||||
use std::num::NonZeroU32;
|
||||
use std::ops::Add;
|
||||
use std::task::{Context, Poll};
|
||||
@ -214,8 +214,11 @@ impl NetworkBehaviour for Relay {
|
||||
_: &ConnectedPoint,
|
||||
_handler: Either<handler::Handler, DummyProtocolsHandler>,
|
||||
) {
|
||||
if let Some(connections) = self.reservations.get_mut(peer) {
|
||||
connections.remove(&connection);
|
||||
if let hash_map::Entry::Occupied(mut peer) = self.reservations.entry(*peer) {
|
||||
peer.get_mut().remove(&connection);
|
||||
if peer.get().is_empty() {
|
||||
peer.remove();
|
||||
}
|
||||
}
|
||||
|
||||
for circuit in self
|
||||
@ -353,9 +356,21 @@ impl NetworkBehaviour for Relay {
|
||||
);
|
||||
}
|
||||
handler::Event::ReservationTimedOut {} => {
|
||||
self.reservations
|
||||
.get_mut(&event_source)
|
||||
.map(|cs| cs.remove(&connection));
|
||||
match self.reservations.entry(event_source) {
|
||||
hash_map::Entry::Occupied(mut peer) => {
|
||||
peer.get_mut().remove(&connection);
|
||||
if peer.get().is_empty() {
|
||||
peer.remove();
|
||||
}
|
||||
}
|
||||
hash_map::Entry::Vacant(_) => {
|
||||
unreachable!(
|
||||
"Expect to track timed out reservation with peer {:?} on connection {:?}",
|
||||
event_source,
|
||||
connection,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
self.queued_actions.push_back(
|
||||
NetworkBehaviourAction::GenerateEvent(Event::ReservationTimedOut {
|
||||
|
Reference in New Issue
Block a user