Skip to content

Commit 68ea5b7

Browse files
authored
fix(relay/client): only try to forward handler events once
We currently try to forward pending handler events in the relay client behavior twice: in `handle_established_{in, out}bound_connection` and in `on_swarm_event()` (introduced in #3328). This is redundant because by the second time we try to forward it there won't be any pending event anymore. This PR removes the duplicated logic from `on_swarm_event`. Forwarding it in `handle_established_{in, out}bound_connection` is more convenient as we still have direct access to the handler there and don't need to go through the swarm. However, it requires that in `handle_established_{in, out}bound_connection` the event is also removed when the connection is relayed, in which case the event is simply discarded because the commands are only valid on direct connections. Pull-Request: #5765.
1 parent 0b44564 commit 68ea5b7

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ libp2p-ping = { version = "0.46.0", path = "protocols/ping" }
9494
libp2p-plaintext = { version = "0.43.0", path = "transports/plaintext" }
9595
libp2p-pnet = { version = "0.26.0", path = "transports/pnet" }
9696
libp2p-quic = { version = "0.12.0", path = "transports/quic" }
97-
libp2p-relay = { version = "0.19.0", path = "protocols/relay" }
97+
libp2p-relay = { version = "0.19.1", path = "protocols/relay" }
9898
libp2p-rendezvous = { version = "0.16.0", path = "protocols/rendezvous" }
9999
libp2p-request-response = { version = "0.28.0", path = "protocols/request-response" }
100100
libp2p-server = { version = "0.12.6", path = "misc/server" }

protocols/relay/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.19.1
2+
3+
- Remove duplicated forwarding of pending events to connection handler.
4+
15
## 0.19.0
26

37
- Deprecate `void` crate.

protocols/relay/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-relay"
33
edition = "2021"
44
rust-version = { workspace = true }
55
description = "Communications relaying for libp2p"
6-
version = "0.19.0"
6+
version = "0.19.1"
77
authors = ["Parity Technologies <[email protected]>", "Max Inden <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

protocols/relay/src/priv_client.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,14 @@ impl NetworkBehaviour for Behaviour {
168168
local_addr: &Multiaddr,
169169
remote_addr: &Multiaddr,
170170
) -> Result<THandler<Self>, ConnectionDenied> {
171+
let pending_handler_command = self.pending_handler_commands.remove(&connection_id);
172+
171173
if local_addr.is_relayed() {
172174
return Ok(Either::Right(dummy::ConnectionHandler));
173175
}
174176
let mut handler = Handler::new(self.local_peer_id, peer, remote_addr.clone());
175177

176-
if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
178+
if let Some(event) = pending_handler_command {
177179
handler.on_behaviour_event(event)
178180
}
179181

@@ -188,13 +190,15 @@ impl NetworkBehaviour for Behaviour {
188190
_: Endpoint,
189191
_: PortUse,
190192
) -> Result<THandler<Self>, ConnectionDenied> {
193+
let pending_handler_command = self.pending_handler_commands.remove(&connection_id);
194+
191195
if addr.is_relayed() {
192196
return Ok(Either::Right(dummy::ConnectionHandler));
193197
}
194198

195199
let mut handler = Handler::new(self.local_peer_id, peer, addr.clone());
196200

197-
if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
201+
if let Some(event) = pending_handler_command {
198202
handler.on_behaviour_event(event)
199203
}
200204

@@ -208,21 +212,11 @@ impl NetworkBehaviour for Behaviour {
208212
connection_id,
209213
endpoint,
210214
..
211-
}) => {
212-
if !endpoint.is_relayed() {
213-
self.directly_connected_peers
214-
.entry(peer_id)
215-
.or_default()
216-
.push(connection_id);
217-
}
218-
219-
if let Some(event) = self.pending_handler_commands.remove(&connection_id) {
220-
self.queued_actions.push_back(ToSwarm::NotifyHandler {
221-
peer_id,
222-
handler: NotifyHandler::One(connection_id),
223-
event: Either::Left(event),
224-
})
225-
}
215+
}) if !endpoint.is_relayed() => {
216+
self.directly_connected_peers
217+
.entry(peer_id)
218+
.or_default()
219+
.push(connection_id);
226220
}
227221
FromSwarm::ConnectionClosed(connection_closed) => {
228222
self.on_connection_closed(connection_closed)

0 commit comments

Comments
 (0)