Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/dcutr: Upgrade at most one inbound connect request #2695

Merged
merged 2 commits into from Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions protocols/dcutr/CHANGELOG.md
@@ -1,3 +1,7 @@
# 0.3.1 - unreleased

- Upgrade at most one inbound connect request.

# 0.3.0

- Update to `libp2p-core` `v0.33.0`.
Expand Down
2 changes: 1 addition & 1 deletion protocols/dcutr/Cargo.toml
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-dcutr"
edition = "2021"
rust-version = "1.56.1"
description = "Direct connection upgrade through relay"
version = "0.3.0"
version = "0.3.1"
authors = ["Max Inden <mail@max-inden.de>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
24 changes: 15 additions & 9 deletions protocols/dcutr/src/handler/relayed.rs
Expand Up @@ -22,7 +22,6 @@

use crate::protocol;
use futures::future::{BoxFuture, FutureExt};
use futures::stream::{FuturesUnordered, StreamExt};
use instant::Instant;
use libp2p_core::either::{EitherError, EitherOutput};
use libp2p_core::multiaddr::Multiaddr;
Expand Down Expand Up @@ -144,10 +143,9 @@ pub struct Handler {
<Self as ConnectionHandler>::Error,
>,
>,
/// Inbound connects, accepted by the behaviour, pending completion.
inbound_connects: FuturesUnordered<
BoxFuture<'static, Result<Vec<Multiaddr>, protocol::inbound::UpgradeError>>,
>,
/// Inbound connect, accepted by the behaviour, pending completion.
inbound_connect:
Option<BoxFuture<'static, Result<Vec<Multiaddr>, protocol::inbound::UpgradeError>>>,
keep_alive: KeepAlive,
}

Expand All @@ -157,7 +155,7 @@ impl Handler {
endpoint,
pending_error: Default::default(),
queued_events: Default::default(),
inbound_connects: Default::default(),
inbound_connect: Default::default(),
keep_alive: KeepAlive::Until(Instant::now() + Duration::from_secs(30)),
}
}
Expand Down Expand Up @@ -247,8 +245,15 @@ impl ConnectionHandler for Handler {
inbound_connect,
obs_addrs,
} => {
self.inbound_connects
.push(inbound_connect.accept(obs_addrs).boxed());
if let Some(_) = self
.inbound_connect
.replace(inbound_connect.accept(obs_addrs).boxed())
{
log::warn!(
"New inbound connect stream while still upgrading previous one. \
Replacing previous with new.",
);
}
}
Command::UpgradeFinishedDontKeepAlive => {
self.keep_alive = KeepAlive::No;
Expand Down Expand Up @@ -364,7 +369,8 @@ impl ConnectionHandler for Handler {
return Poll::Ready(event);
}

while let Poll::Ready(Some(result)) = self.inbound_connects.poll_next_unpin(cx) {
if let Some(Poll::Ready(result)) = self.inbound_connect.as_mut().map(|f| f.poll_unpin(cx)) {
self.inbound_connect = None;
match result {
Ok(addresses) => {
return Poll::Ready(ConnectionHandlerEvent::Custom(
Expand Down