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

fix(swarm): keep connections alive while active streams exist #4595

Merged
merged 56 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
e5aaa04
add no_keep_alive fn to Stream and counter to Connection
leonzchang Oct 5, 2023
55525cc
call fn no_keep_alive when ping protocol stream received
leonzchang Oct 6, 2023
340ecc9
apply suggested fixes
leonzchang Oct 6, 2023
55b2c6d
apply suggested fixes
leonzchang Oct 11, 2023
e07e274
update root Cargo.toml
leonzchang Oct 11, 2023
0fc1a2a
fix rustdoc intra-doc links
leonzchang Oct 11, 2023
f5a7cd4
fix tests
leonzchang Oct 11, 2023
b9f0039
compute the shutdown only when negotiated streams are finished
leonzchang Oct 12, 2023
346ba26
remove stream tracking code in protocols
leonzchang Oct 12, 2023
9dc3ff3
apply suggested fixes
leonzchang Oct 13, 2023
c1b5d98
correct crate version
leonzchang Oct 16, 2023
3006d84
add allow deprecated to compute_new_shutdown
leonzchang Oct 18, 2023
8b276e5
fix rustfmt
leonzchang Oct 18, 2023
7b29afe
update libp2p-noise version in root Cargo.toml
leonzchang Oct 18, 2023
9cc7d93
Merge branch 'master' into feat/stream-counter
thomaseizinger Oct 20, 2023
2ed8071
Fix compile error
thomaseizinger Oct 20, 2023
ad8104d
Always compute shutdown
thomaseizinger Oct 20, 2023
8f34ba8
Merge branch 'master' into feat/stream-counter
leonzchang Oct 20, 2023
5db7968
fix checking keep alive status condition in relay
leonzchang Oct 20, 2023
aeb0480
Merge branch 'master' into feat/stream-counter
leonzchang Oct 20, 2023
464b803
Merge branch 'master' into feat/stream-counter
leonzchang Oct 20, 2023
021da14
dispatch no_keep_alive on outbound streams in ping protocols
leonzchang Oct 20, 2023
9c049e6
Merge branch 'master' into feat/stream-counter
leonzchang Oct 20, 2023
f6531aa
remove protocols keep alive stream check
leonzchang Oct 20, 2023
b8dea1e
Merge branch 'master' into feat/stream-counter
leonzchang Oct 20, 2023
ae19cc2
fix idiomatic return
leonzchang Oct 20, 2023
1837ad6
Merge branch 'master' into feat/stream-counter
leonzchang Oct 22, 2023
77b840e
apply partial suggested fixes
leonzchang Oct 22, 2023
b9c37fc
add connection_keep_alive default impl
leonzchang Oct 22, 2023
8a22bc8
update changelog & docs
leonzchang Oct 22, 2023
7401ed2
remove the remaining KeepAlive::Until & related test
leonzchang Oct 22, 2023
20a6582
remove unuse import & KeepAlive::Until related tests
leonzchang Oct 22, 2023
c8481fe
Merge branch 'feat/stream-counter' into feat/ConnectionHandler-defaul…
leonzchang Oct 24, 2023
436c082
Merge pull request #1 from leonzchang/feat/ConnectionHandler-default-…
leonzchang Oct 24, 2023
3e75728
apply suggested fixes & remove KeepAlive
leonzchang Oct 24, 2023
5efd3d9
Merge branch 'master' into feat/stream-counter
leonzchang Oct 24, 2023
0b53588
fix clippy lint
leonzchang Oct 24, 2023
991ef47
fix clippy lint
leonzchang Oct 24, 2023
18b7314
fix clippy lint
leonzchang Oct 24, 2023
6fc1b0b
Merge branch 'master' into feat/stream-counter
leonzchang Oct 24, 2023
69a4a8b
revert 3e75728 wrong docs
leonzchang Oct 24, 2023
3d10eb4
apply suggested fixes
leonzchang Oct 24, 2023
c79537b
Merge branch 'master' into feat/stream-counter
leonzchang Oct 24, 2023
47a2b4e
fixed test
leonzchang Oct 24, 2023
4ad1117
Update docs
thomaseizinger Oct 24, 2023
c963452
Remove obsolete docs
thomaseizinger Oct 24, 2023
a58584d
Update changelog
thomaseizinger Oct 24, 2023
0d02ccc
Fix doc link
thomaseizinger Oct 24, 2023
0c9e3ee
replace Arc<()> to ActiveStreamCounter in swarm
leonzchang Oct 24, 2023
92823e1
apply suggested fixes
leonzchang Oct 25, 2023
16548b1
Merge branch 'master' into feat/stream-counter
leonzchang Oct 25, 2023
1aaa6a0
merge completed
leonzchang Oct 25, 2023
17d4513
Merge branch 'master' into feat/stream-counter
leonzchang Oct 25, 2023
51c2df6
Merge branch 'master' into feat/stream-counter
leonzchang Oct 25, 2023
41662a4
wrap Arc to a new type & refactor stream ignore_for_keep_alive
leonzchang Oct 25, 2023
46d8216
fix clippy error
leonzchang Oct 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions protocols/dcutr/src/handler/relayed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use libp2p_swarm::handler::{
ListenUpgradeError,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, StreamUpgradeError, SubstreamProtocol,
};
use std::collections::VecDeque;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -249,20 +249,12 @@ impl ConnectionHandler for Handler {
}
}

fn connection_keep_alive(&self) -> KeepAlive {
if !self.queued_events.is_empty() {
return KeepAlive::Yes;
}

if self.inbound_connect.is_some() {
return KeepAlive::Yes;
}

fn connection_keep_alive(&self) -> bool {
if self.attempts < MAX_NUMBER_OF_UPGRADE_ATTEMPTS {
return KeepAlive::Yes;
return true;
}

KeepAlive::No
false
}

fn poll(
Expand Down
2 changes: 0 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3265,7 +3265,6 @@ where
type ConnectionHandler = Handler;
type ToSwarm = Event;

#[allow(deprecated)]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
fn handle_established_inbound_connection(
&mut self,
_: ConnectionId,
Expand All @@ -3276,7 +3275,6 @@ where
Ok(Handler::new(self.config.protocol_config()))
}

#[allow(deprecated)]
fn handle_established_outbound_connection(
&mut self,
_: ConnectionId,
Expand Down
25 changes: 3 additions & 22 deletions protocols/gossipsub/src/handler.rs
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ use instant::Instant;
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_swarm::handler::{
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, StreamUpgradeError,
SubstreamProtocol,
FullyNegotiatedInbound, FullyNegotiatedOutbound, StreamUpgradeError, SubstreamProtocol,
};
use libp2p_swarm::Stream;
use smallvec::SmallVec;
Expand Down Expand Up @@ -424,26 +423,8 @@ impl ConnectionHandler for Handler {
}
}

fn connection_keep_alive(&self) -> KeepAlive {
match self {
Handler::Enabled(handler) => {
if handler.in_mesh {
return KeepAlive::Yes;
}

if let Some(
OutboundSubstreamState::PendingSend(_, _)
| OutboundSubstreamState::PendingFlush(_),
) = handler.outbound_substream
{
return KeepAlive::Yes;
}

#[allow(deprecated)]
KeepAlive::No
}
Handler::Disabled(_) => KeepAlive::No,
}
fn connection_keep_alive(&self) -> bool {
matches!(self, Handler::Enabled(h) if h.in_mesh)
}
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

fn poll(
Expand Down
10 changes: 1 addition & 9 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use libp2p_swarm::handler::{
ProtocolSupport,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamProtocol, StreamUpgradeError,
ConnectionHandler, ConnectionHandlerEvent, StreamProtocol, StreamUpgradeError,
SubstreamProtocol, SupportedProtocols,
};
use log::{warn, Level};
Expand Down Expand Up @@ -314,14 +314,6 @@ impl ConnectionHandler for Handler {
}
}

fn connection_keep_alive(&self) -> KeepAlive {
if !self.active_streams.is_empty() {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
return KeepAlive::Yes;
}

KeepAlive::No
}

fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
10 changes: 1 addition & 9 deletions protocols/kad/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionId, KeepAlive, Stream, StreamUpgradeError,
ConnectionHandler, ConnectionHandlerEvent, ConnectionId, Stream, StreamUpgradeError,
SubstreamProtocol, SupportedProtocols,
};
use log::trace;
Expand Down Expand Up @@ -702,14 +702,6 @@ impl ConnectionHandler for Handler {
}
}

fn connection_keep_alive(&self) -> KeepAlive {
if self.outbound_substreams.is_empty() && self.inbound_substreams.is_empty() {
return KeepAlive::No;
};

KeepAlive::Yes
}

fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down
14 changes: 6 additions & 8 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, Stream, StreamProtocol,
StreamUpgradeError, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, Stream, StreamProtocol, StreamUpgradeError,
SubstreamProtocol,
};
use std::collections::VecDeque;
use std::{
Expand Down Expand Up @@ -225,10 +225,6 @@ impl ConnectionHandler for Handler {

fn on_behaviour_event(&mut self, _: Void) {}

fn connection_keep_alive(&self) -> KeepAlive {
KeepAlive::No
}

fn poll(
&mut self,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -349,15 +345,17 @@ impl ConnectionHandler for Handler {
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
protocol: stream,
protocol: mut stream,
..
}) => {
stream.ignore_for_keep_alive();
self.inbound = Some(protocol::recv_ping(stream).boxed());
}
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol: stream,
protocol: mut stream,
..
}) => {
stream.ignore_for_keep_alive();
self.outbound = Some(OutboundState::Ping(
send_ping(stream, self.config.timeout).boxed(),
));
Expand Down
27 changes: 8 additions & 19 deletions protocols/relay/src/behaviour/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionId, KeepAlive, Stream, StreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, ConnectionId, Stream, StreamProtocol,
StreamUpgradeError, SubstreamProtocol,
};
use std::collections::VecDeque;
Expand Down Expand Up @@ -376,10 +376,6 @@ pub struct Handler {
///
/// Contains a [`futures::future::Future`] for each lend out substream that
/// resolves once the substream is dropped.
///
/// Once all substreams are dropped and this handler has no other work,
/// [`KeepAlive::Until`] can be set, allowing the connection to be closed
/// eventually.
alive_lend_out_substreams: FuturesUnordered<oneshot::Receiver<()>>,
/// Futures relaying data for circuit between two peers.
circuits: Futures<(CircuitId, PeerId, Result<(), std::io::Error>)>,
Expand Down Expand Up @@ -615,13 +611,12 @@ impl ConnectionHandler for Handler {
}
}

fn connection_keep_alive(&self) -> KeepAlive {
match self.idle_at {
Some(idle_at) if Instant::now().duration_since(idle_at) > Duration::from_secs(10) => {
KeepAlive::No
}
_ => KeepAlive::Yes,
}
fn connection_keep_alive(&self) -> bool {
let Some(idle_at) = self.idle_at else {
return true;
};

Instant::now().duration_since(idle_at) <= Duration::from_secs(10)
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
}

fn poll(
Expand Down Expand Up @@ -881,13 +876,7 @@ impl ConnectionHandler for Handler {
{}

// Check keep alive status.
if self.reservation_request_future.is_none()
&& self.circuit_accept_futures.is_empty()
&& self.circuit_deny_futures.is_empty()
&& self.alive_lend_out_substreams.is_empty()
&& self.circuits.is_empty()
&& self.active_reservation.is_none()
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
{
if self.active_reservation.is_none() {
if self.idle_at.is_none() {
self.idle_at = Some(Instant::now());
}
Expand Down
26 changes: 3 additions & 23 deletions protocols/relay/src/priv_client/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamProtocol, StreamUpgradeError,
ConnectionHandler, ConnectionHandlerEvent, StreamProtocol, StreamUpgradeError,
SubstreamProtocol,
};
use log::debug;
Expand Down Expand Up @@ -319,28 +319,8 @@ impl ConnectionHandler for Handler {
}
}

fn connection_keep_alive(&self) -> KeepAlive {
if self.reservation.is_some() {
return KeepAlive::Yes;
}

if !self.alive_lend_out_substreams.is_empty() {
return KeepAlive::Yes;
}

if !self.circuit_deny_futs.is_empty() {
return KeepAlive::Yes;
}

if !self.open_circuit_futs.is_empty() {
return KeepAlive::Yes;
}

if !self.outbound_circuits.is_empty() {
return KeepAlive::Yes;
}

KeepAlive::No
fn connection_keep_alive(&self) -> bool {
self.reservation.is_some()
}

fn poll(
Expand Down
18 changes: 1 addition & 17 deletions protocols/request-response/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use libp2p_swarm::handler::{
ListenUpgradeError,
};
use libp2p_swarm::{
handler::{ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError},
handler::{ConnectionHandler, ConnectionHandlerEvent, StreamUpgradeError},
SubstreamProtocol,
};
use smallvec::SmallVec;
Expand All @@ -59,8 +59,6 @@ where
/// The timeout for inbound and outbound substreams (i.e. request
/// and response processing).
substream_timeout: Duration,
/// The current connection keep-alive.
keep_alive: KeepAlive,
/// Queue of events to emit in `poll()`.
pending_events: VecDeque<Event<TCodec>>,
/// Outbound upgrades waiting to be emitted as an `OutboundSubstreamRequest`.
Expand Down Expand Up @@ -94,7 +92,6 @@ where
Self {
inbound_protocols,
codec,
keep_alive: KeepAlive::Yes,
substream_timeout,
outbound: VecDeque::new(),
inbound: FuturesUnordered::new(),
Expand Down Expand Up @@ -274,14 +271,9 @@ where
}

fn on_behaviour_event(&mut self, request: Self::FromBehaviour) {
self.keep_alive = KeepAlive::Yes;
self.outbound.push_back(request);
}

fn connection_keep_alive(&self) -> KeepAlive {
self.keep_alive
}

fn poll(
&mut self,
cx: &mut Context<'_>,
Expand All @@ -300,7 +292,6 @@ where
match result {
Ok(((id, rq), rs_sender)) => {
// We received an inbound request.
self.keep_alive = KeepAlive::Yes;
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Event::Request {
request_id: id,
request: rq,
Expand Down Expand Up @@ -330,13 +321,6 @@ where
self.outbound.shrink_to_fit();
}

if self.inbound.is_empty() && self.keep_alive.is_yes() {
// No new inbound or outbound requests. We already check
// there is no active streams exist in swarm connection,
// so we can set keep-alive to no directly.
self.keep_alive = KeepAlive::No;
}

Poll::Pending
}

Expand Down
7 changes: 3 additions & 4 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ use crate::behaviour::FromSwarm;
use crate::connection::ConnectionId;
use crate::handler::{
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, ListenUpgradeError,
SubstreamProtocol,
FullyNegotiatedInbound, FullyNegotiatedOutbound, ListenUpgradeError, SubstreamProtocol,
};
use crate::upgrade::SendWrapper;
use crate::{
Expand Down Expand Up @@ -291,11 +290,11 @@ where
.on_behaviour_event(event)
}

fn connection_keep_alive(&self) -> KeepAlive {
fn connection_keep_alive(&self) -> bool {
self.inner
.as_ref()
.map(|h| h.connection_keep_alive())
.unwrap_or(KeepAlive::No)
.unwrap_or(false)
}

fn poll(
Expand Down
Loading