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

feat(swarm): add ConnectionHandler::connection_keep_alive default implementation #4703

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 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
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
4 changes: 0 additions & 4 deletions protocols/dcutr/src/handler/relayed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,6 @@ impl ConnectionHandler for Handler {
return KeepAlive::Yes;
}

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

if self.attempts < MAX_NUMBER_OF_UPGRADE_ATTEMPTS {
return KeepAlive::Yes;
}
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)]
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
9 changes: 0 additions & 9 deletions protocols/gossipsub/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,6 @@ impl ConnectionHandler for Handler {
return KeepAlive::Yes;
}

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

#[allow(deprecated)]
KeepAlive::No
}
Handler::Disabled(_) => KeepAlive::No,
Expand Down
4 changes: 0 additions & 4 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,6 @@ impl ConnectionHandler for Handler {
}

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

KeepAlive::No
}

Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn build_node_with_config(cfg: Config) -> (Multiaddr, TestSwarm) {
behaviour,
local_id,
swarm::Config::with_async_std_executor()
.with_idle_connection_timeout(Duration::from_secs(5)),
.with_idle_connection_timeout(Duration::from_secs(10)),
);

let address: Multiaddr = Protocol::Memory(random::<u64>()).into();
Expand Down
6 changes: 1 addition & 5 deletions protocols/kad/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,7 @@ 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
KeepAlive::No
}

fn poll(
Expand Down
10 changes: 7 additions & 3 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ impl ConnectionHandler for Handler {
log::debug!("Inbound ping error: {:?}", e);
self.inbound = None;
}
Poll::Ready(Ok(stream)) => {
Poll::Ready(Ok(mut stream)) => {
log::trace!("answered inbound ping from {}", self.peer);

stream.no_keep_alive();
// A ping from a remote peer has been answered, wait for the next.
self.inbound = Some(protocol::recv_ping(stream).boxed());
}
Expand Down Expand Up @@ -294,9 +295,10 @@ impl ConnectionHandler for Handler {
self.outbound = Some(OutboundState::Ping(ping));
break;
}
Poll::Ready(Ok((stream, rtt))) => {
Poll::Ready(Ok((mut stream, rtt))) => {
log::debug!("latency to {} is {}ms", self.peer, rtt.as_millis());

stream.no_keep_alive();
self.failures = 0;
self.interval.reset(self.config.interval);
self.outbound = Some(OutboundState::Idle(stream));
Expand All @@ -307,12 +309,14 @@ impl ConnectionHandler for Handler {
self.pending_errors.push_front(e);
}
},
Some(OutboundState::Idle(stream)) => match self.interval.poll_unpin(cx) {
Some(OutboundState::Idle(mut stream)) => match self.interval.poll_unpin(cx) {
Poll::Pending => {
stream.no_keep_alive();
self.outbound = Some(OutboundState::Idle(stream));
break;
}
Poll::Ready(()) => {
stream.no_keep_alive();
self.outbound = Some(OutboundState::Ping(
send_ping(stream, self.config.timeout).boxed(),
));
Expand Down
12 changes: 1 addition & 11 deletions protocols/relay/src/behaviour/handler.rs
Original file line number Diff line number Diff line change
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 @@ -881,13 +877,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()
{
if self.active_reservation.is_none() {
if self.idle_at.is_none() {
self.idle_at = Some(Instant::now());
}
Expand Down
16 changes: 0 additions & 16 deletions protocols/relay/src/priv_client/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,6 @@ impl ConnectionHandler for Handler {
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
}

Expand Down
14 changes: 1 addition & 13 deletions protocols/request-response/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,12 +271,11 @@ 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
KeepAlive::No
}

fn poll(
Expand All @@ -300,7 +296,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 +325,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
2 changes: 2 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
See [PR 4225](https://github.com/libp2p/rust-libp2p/pull/4225).
- Remove deprecated `keep_alive_timeout` in `OneShotHandlerConfig`.
See [PR 4677](https://github.com/libp2p/rust-libp2p/pull/4677).
- Add `ConnectionHandler::connection_keep_alive` default implementation that returns `KeepAlive::No`.
See [PR 4703](https://github.com/libp2p/rust-libp2p/pull/4703).

## 0.43.6

Expand Down
Loading
Loading