Skip to content

Commit 59a74b4

Browse files
authored
protocols/dcutr: Upgrade at most one inbound connect request (#2695)
Sending two concurrent connect requests is nonsensical. In the case where a remote sends two, the earlier is dropped in favor of the latter.
1 parent 676a630 commit 59a74b4

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

protocols/dcutr/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.3.1 - unreleased
2+
3+
- Upgrade at most one inbound connect request.
4+
15
# 0.3.0
26

37
- Update to `libp2p-core` `v0.33.0`.

protocols/dcutr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-dcutr"
33
edition = "2021"
44
rust-version = "1.56.1"
55
description = "Direct connection upgrade through relay"
6-
version = "0.3.0"
6+
version = "0.3.1"
77
authors = ["Max Inden <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

protocols/dcutr/src/handler/relayed.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
2323
use crate::protocol;
2424
use futures::future::{BoxFuture, FutureExt};
25-
use futures::stream::{FuturesUnordered, StreamExt};
2625
use instant::Instant;
2726
use libp2p_core::either::{EitherError, EitherOutput};
2827
use libp2p_core::multiaddr::Multiaddr;
@@ -144,10 +143,9 @@ pub struct Handler {
144143
<Self as ConnectionHandler>::Error,
145144
>,
146145
>,
147-
/// Inbound connects, accepted by the behaviour, pending completion.
148-
inbound_connects: FuturesUnordered<
149-
BoxFuture<'static, Result<Vec<Multiaddr>, protocol::inbound::UpgradeError>>,
150-
>,
146+
/// Inbound connect, accepted by the behaviour, pending completion.
147+
inbound_connect:
148+
Option<BoxFuture<'static, Result<Vec<Multiaddr>, protocol::inbound::UpgradeError>>>,
151149
keep_alive: KeepAlive,
152150
}
153151

@@ -157,7 +155,7 @@ impl Handler {
157155
endpoint,
158156
pending_error: Default::default(),
159157
queued_events: Default::default(),
160-
inbound_connects: Default::default(),
158+
inbound_connect: Default::default(),
161159
keep_alive: KeepAlive::Until(Instant::now() + Duration::from_secs(30)),
162160
}
163161
}
@@ -247,8 +245,15 @@ impl ConnectionHandler for Handler {
247245
inbound_connect,
248246
obs_addrs,
249247
} => {
250-
self.inbound_connects
251-
.push(inbound_connect.accept(obs_addrs).boxed());
248+
if let Some(_) = self
249+
.inbound_connect
250+
.replace(inbound_connect.accept(obs_addrs).boxed())
251+
{
252+
log::warn!(
253+
"New inbound connect stream while still upgrading previous one. \
254+
Replacing previous with new.",
255+
);
256+
}
252257
}
253258
Command::UpgradeFinishedDontKeepAlive => {
254259
self.keep_alive = KeepAlive::No;
@@ -364,7 +369,8 @@ impl ConnectionHandler for Handler {
364369
return Poll::Ready(event);
365370
}
366371

367-
while let Poll::Ready(Some(result)) = self.inbound_connects.poll_next_unpin(cx) {
372+
if let Some(Poll::Ready(result)) = self.inbound_connect.as_mut().map(|f| f.poll_unpin(cx)) {
373+
self.inbound_connect = None;
368374
match result {
369375
Ok(addresses) => {
370376
return Poll::Ready(ConnectionHandlerEvent::Custom(

0 commit comments

Comments
 (0)