Skip to content

Commit

Permalink
feat(relay): emit event when client connections are dropped
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RolandSherwin authored Feb 27, 2025
1 parent b29c2f6 commit e244b98
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ 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" }
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" }
Expand Down
4 changes: 4 additions & 0 deletions misc/metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

<!-- Update to libp2p-core v0.43.0 -->
Expand Down
2 changes: 1 addition & 1 deletion misc/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mail@max-inden.de>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
2 changes: 2 additions & 0 deletions misc/metrics/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum EventType {
ReservationReqAcceptFailed,
ReservationReqDenied,
ReservationReqDenyFailed,
ReservationClosed,
ReservationTimedOut,
CircuitReqDenied,
CircuitReqDenyFailed,
Expand All @@ -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)]
Expand Down
4 changes: 3 additions & 1 deletion protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

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 = { workspace = true }
description = "Communications relaying for libp2p"
version = "0.19.1"
version = "0.20.0"
authors = ["Parity Technologies <admin@parity.io>", "Max Inden <mail@max-inden.de>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
9 changes: 8 additions & 1 deletion protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit e244b98

Please sign in to comment.