Skip to content

Commit

Permalink
protocols/relay/src/v2: Remove empty peer entries in self.reservations (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
mxinden authored Feb 1, 2022
1 parent 13ded7f commit 6338b25
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down Expand Up @@ -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" }
Expand Down
6 changes: 6 additions & 0 deletions protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>", "Max Inden <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
27 changes: 21 additions & 6 deletions protocols/relay/src/v2/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6338b25

Please sign in to comment.