From e244b984f8bae815f4d0977c3713c40ff73aa089 Mon Sep 17 00:00:00 2001 From: RolandSherwin Date: Fri, 28 Feb 2025 05:11:34 +0530 Subject: [PATCH] feat(relay): emit event when client connections are dropped When a relay server has no more connection with a reserved client, it would remove the reservation and the drop the circuits without any information passed to the server. It will be useful to for a server to track all its reservations and to know when they're removed (without keeping track of the connections themselves). This PR aims to notify the server when a reservation closes, with the emission of the following event ``` /// A reservation has been closed. ReservationClosed { src_peer_id: PeerId }, ``` Pull-Request: #5869. --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- misc/metrics/CHANGELOG.md | 4 ++++ misc/metrics/Cargo.toml | 2 +- misc/metrics/src/relay.rs | 2 ++ protocols/relay/CHANGELOG.md | 4 +++- protocols/relay/Cargo.toml | 2 +- protocols/relay/src/behaviour.rs | 9 ++++++++- 8 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02e079a2876..d812fc0fa4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3017,7 +3017,7 @@ dependencies = [ [[package]] name = "libp2p-metrics" -version = "0.16.0" +version = "0.16.1" dependencies = [ "futures", "libp2p-core", @@ -3204,7 +3204,7 @@ dependencies = [ [[package]] name = "libp2p-relay" -version = "0.19.1" +version = "0.20.0" dependencies = [ "asynchronous-codec", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 5c09580a884..f6220cece5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,7 +86,7 @@ libp2p-identity = { version = "0.2.10" } libp2p-kad = { version = "0.47.0", path = "protocols/kad" } libp2p-mdns = { version = "0.47.0", path = "protocols/mdns" } libp2p-memory-connection-limits = { version = "0.4.0", path = "misc/memory-connection-limits" } -libp2p-metrics = { version = "0.16.0", path = "misc/metrics" } +libp2p-metrics = { version = "0.16.1", path = "misc/metrics" } libp2p-mplex = { version = "0.43.1", path = "muxers/mplex" } libp2p-noise = { version = "0.46.0", path = "transports/noise" } libp2p-perf = { version = "0.4.0", path = "protocols/perf" } @@ -94,7 +94,7 @@ libp2p-ping = { version = "0.46.0", path = "protocols/ping" } libp2p-plaintext = { version = "0.43.0", path = "transports/plaintext" } libp2p-pnet = { version = "0.26.0", path = "transports/pnet" } libp2p-quic = { version = "0.12.0", path = "transports/quic" } -libp2p-relay = { version = "0.19.1", path = "protocols/relay" } +libp2p-relay = { version = "0.20.0", path = "protocols/relay" } libp2p-rendezvous = { version = "0.16.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.28.1", path = "protocols/request-response" } libp2p-server = { version = "0.12.6", path = "misc/server" } diff --git a/misc/metrics/CHANGELOG.md b/misc/metrics/CHANGELOG.md index fa57d50fa7a..c6ad96c1ad9 100644 --- a/misc/metrics/CHANGELOG.md +++ b/misc/metrics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.16.1 +- Add `ReservationClosed` as a relay metric. + See [PR 5869](https://github.com/libp2p/rust-libp2p/pull/5869). + ## 0.16.0 diff --git a/misc/metrics/Cargo.toml b/misc/metrics/Cargo.toml index 97cc581c671..fd72fc84fe4 100644 --- a/misc/metrics/Cargo.toml +++ b/misc/metrics/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-metrics" edition = "2021" rust-version = { workspace = true } description = "Metrics for libp2p" -version = "0.16.0" +version = "0.16.1" authors = ["Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/misc/metrics/src/relay.rs b/misc/metrics/src/relay.rs index d4c25b6eb3e..c6b3827743c 100644 --- a/misc/metrics/src/relay.rs +++ b/misc/metrics/src/relay.rs @@ -54,6 +54,7 @@ enum EventType { ReservationReqAcceptFailed, ReservationReqDenied, ReservationReqDenyFailed, + ReservationClosed, ReservationTimedOut, CircuitReqDenied, CircuitReqDenyFailed, @@ -76,6 +77,7 @@ impl From<&libp2p_relay::Event> for EventType { libp2p_relay::Event::ReservationReqDenyFailed { .. } => { EventType::ReservationReqDenyFailed } + libp2p_relay::Event::ReservationClosed { .. } => EventType::ReservationClosed, libp2p_relay::Event::ReservationTimedOut { .. } => EventType::ReservationTimedOut, libp2p_relay::Event::CircuitReqDenied { .. } => EventType::CircuitReqDenied, #[allow(deprecated)] diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index b45f25c5f0d..d14e567137b 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,6 +1,8 @@ -## 0.19.1 +## 0.20.0 - Remove duplicated forwarding of pending events to connection handler. +- Emit `relay::Event::ReservationClosed` when an active reservation is dropped due to the connection closing. + See [PR 5869](https://github.com/libp2p/rust-libp2p/pull/5869). ## 0.19.0 diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index bd34fa7ee34..5515d0f1527 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-relay" edition = "2021" rust-version = { workspace = true } description = "Communications relaying for libp2p" -version = "0.19.1" +version = "0.20.0" authors = ["Parity Technologies ", "Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 968642b3f1f..5083d9e14a0 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -193,6 +193,8 @@ pub enum Event { src_peer_id: PeerId, error: inbound_hop::Error, }, + /// A reservation has been closed. + ReservationClosed { src_peer_id: PeerId }, /// An inbound reservation has timed out. ReservationTimedOut { src_peer_id: PeerId }, /// An inbound circuit request has been denied. @@ -277,7 +279,12 @@ impl Behaviour { }: ConnectionClosed, ) { if let hash_map::Entry::Occupied(mut peer) = self.reservations.entry(peer_id) { - peer.get_mut().remove(&connection_id); + if peer.get_mut().remove(&connection_id) { + self.queued_actions + .push_back(ToSwarm::GenerateEvent(Event::ReservationClosed { + src_peer_id: peer_id, + })); + } if peer.get().is_empty() { peer.remove(); }