From 52e82f19aadfecec0078924a01fa37b9ad2bad4c Mon Sep 17 00:00:00 2001 From: Max Inden Date: Mon, 31 Jan 2022 12:21:30 +0100 Subject: [PATCH 1/2] protocols/relay/src/v2: Remove empty peer entries in self.reservations 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. --- protocols/relay/src/v2/relay.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/protocols/relay/src/v2/relay.rs b/protocols/relay/src/v2/relay.rs index eae734f3c3e..ae00ff22fdd 100644 --- a/protocols/relay/src/v2/relay.rs +++ b/protocols/relay/src/v2/relay.rs @@ -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, ) { - 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 { From 34315e8c32cf0b81cfd6be14fc773c67a73a1d48 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Mon, 31 Jan 2022 12:33:48 +0100 Subject: [PATCH 2/2] *: Update versions and add changelog entries --- CHANGELOG.md | 5 +++++ Cargo.toml | 4 ++-- protocols/relay/CHANGELOG.md | 6 ++++++ protocols/relay/Cargo.toml | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f24fd59974f..3fad20083f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,11 @@ # `libp2p` facade crate +## Version 0.42.1 [unreleased] + +- Update individual crates. + - `libp2p-relay` + ## Version 0.42.0 [2022-01-27] - Update individual crates. diff --git a/Cargo.toml b/Cargo.toml index 345b26077d6..92e5e095bbd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p" edition = "2021" rust-version = "1.56.1" description = "Peer-to-peer networking library" -version = "0.42.0" +version = "0.42.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -88,7 +88,7 @@ libp2p-noise = { version = "0.34.0", path = "transports/noise", optional = true libp2p-ping = { version = "0.33.0", path = "protocols/ping", optional = true } libp2p-plaintext = { version = "0.31.0", path = "transports/plaintext", optional = true } libp2p-pnet = { version = "0.22.0", path = "transports/pnet", optional = true } -libp2p-relay = { version = "0.6.0", path = "protocols/relay", optional = true } +libp2p-relay = { version = "0.6.1", path = "protocols/relay", optional = true } libp2p-rendezvous = { version = "0.3.0", path = "protocols/rendezvous", optional = true } libp2p-request-response = { version = "0.15.0", path = "protocols/request-response", optional = true } libp2p-swarm = { version = "0.33.0", path = "swarm" } diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index 2f2f5df1805..abc74a03eca 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.6.1 [unreleased] + +- Remove empty peer entries in `reservations` `HashMap`. See [PR 2464]. + +[PR 2464]: https://github.com/libp2p/rust-libp2p/pull/2464 + # 0.6.0 [2022-01-27] - Update dependencies. diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 8e7ca1b3726..c5e8add51ba 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-relay" edition = "2021" rust-version = "1.56.1" description = "Communications relaying for libp2p" -version = "0.6.0" +version = "0.6.1" authors = ["Parity Technologies ", "Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p"