From 36ec56cf788d2cfe584630b979d294393cd4ed93 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Tue, 17 Oct 2023 15:46:30 +0800 Subject: [PATCH 1/5] remove KeepAlive::Until & update changelogs --- protocols/relay/CHANGELOG.md | 2 +- protocols/relay/src/behaviour/handler.rs | 12 +----------- protocols/relay/tests/lib.rs | 2 +- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index 6c5e107e67d..2cb09ca9f2c 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -2,7 +2,7 @@ diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index fc822e78ce5..348853e7a55 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -29,7 +29,6 @@ use futures::future::{BoxFuture, FutureExt, TryFutureExt}; use futures::io::AsyncWriteExt; use futures::stream::{FuturesUnordered, StreamExt}; use futures_timer::Delay; -use instant::Instant; use libp2p_core::upgrade::ReadyUpgrade; use libp2p_core::{ConnectedPoint, Multiaddr}; use libp2p_identity::PeerId; @@ -883,16 +882,7 @@ impl ConnectionHandler for Handler { && self.circuits.is_empty() && self.active_reservation.is_none() { - #[allow(deprecated)] - match self.keep_alive { - KeepAlive::Yes => { - self.keep_alive = KeepAlive::Until(Instant::now() + Duration::from_secs(10)); - } - KeepAlive::Until(_) => {} - KeepAlive::No => panic!("Handler never sets KeepAlive::No."), - } - } else { - self.keep_alive = KeepAlive::Yes; + self.keep_alive = KeepAlive::No } Poll::Pending diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index f5c834bcb37..180891029ee 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -323,7 +323,7 @@ fn build_relay() -> Swarm { ), }, local_peer_id, - Config::with_async_std_executor(), + Config::with_async_std_executor().with_idle_connection_timeout(Duration::from_secs(10)), ) } From c2251ff17a0003c69e2070c555e818f95653dc48 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 18 Oct 2023 11:21:22 +1100 Subject: [PATCH 2/5] Rework keep-alive handling --- protocols/relay/src/behaviour/handler.rs | 25 +++++++++++++++++++----- protocols/relay/tests/lib.rs | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 348853e7a55..fa05999e7be 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -29,6 +29,7 @@ use futures::future::{BoxFuture, FutureExt, TryFutureExt}; use futures::io::AsyncWriteExt; use futures::stream::{FuturesUnordered, StreamExt}; use futures_timer::Delay; +use instant::Instant; use libp2p_core::upgrade::ReadyUpgrade; use libp2p_core::{ConnectedPoint, Multiaddr}; use libp2p_identity::PeerId; @@ -354,8 +355,8 @@ pub struct Handler { >, >, - /// Until when to keep the connection alive. - keep_alive: KeepAlive, + /// The point in time when this connection started idleing. + idle_at: Option, /// Future handling inbound reservation request. reservation_request_future: Option, @@ -410,13 +411,13 @@ impl Handler { config, queued_events: Default::default(), pending_error: Default::default(), + idle_at: None, reservation_request_future: Default::default(), circuit_accept_futures: Default::default(), circuit_deny_futures: Default::default(), alive_lend_out_substreams: Default::default(), circuits: Default::default(), active_reservation: Default::default(), - keep_alive: KeepAlive::Yes, pending_connect_requests: Default::default(), } } @@ -615,7 +616,17 @@ impl ConnectionHandler for Handler { } fn connection_keep_alive(&self) -> KeepAlive { - self.keep_alive + // Only inbound connections need to be kept alive longer than they are active. + if self.endpoint.is_dialer() { + return KeepAlive::No; + } + + match self.idle_at { + Some(idle_at) if Instant::now().duration_since(idle_at) > Duration::from_secs(10) => { + KeepAlive::No + } + _ => KeepAlive::Yes, + } } fn poll( @@ -882,7 +893,11 @@ impl ConnectionHandler for Handler { && self.circuits.is_empty() && self.active_reservation.is_none() { - self.keep_alive = KeepAlive::No + if self.idle_at.is_none() { + self.idle_at = Some(Instant::now()); + } + } else { + self.idle_at = None; } Poll::Pending diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index 180891029ee..f5c834bcb37 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -323,7 +323,7 @@ fn build_relay() -> Swarm { ), }, local_peer_id, - Config::with_async_std_executor().with_idle_connection_timeout(Duration::from_secs(10)), + Config::with_async_std_executor(), ) } From a441b79e618c9483900b011f1e9d17e839c237db Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 20 Oct 2023 10:53:34 +1100 Subject: [PATCH 3/5] Don't differentiate between in and outbound connections --- protocols/relay/src/behaviour/handler.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index fa05999e7be..2b22e66d21c 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -616,10 +616,6 @@ impl ConnectionHandler for Handler { } fn connection_keep_alive(&self) -> KeepAlive { - // Only inbound connections need to be kept alive longer than they are active. - if self.endpoint.is_dialer() { - return KeepAlive::No; - } match self.idle_at { Some(idle_at) if Instant::now().duration_since(idle_at) > Duration::from_secs(10) => { From da007a697cba5e09e2adb057fa2e66849bec42bb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 20 Oct 2023 15:24:50 +1100 Subject: [PATCH 4/5] Fix formatting --- protocols/relay/src/behaviour/handler.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 2b22e66d21c..6fb0a834d2f 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -616,7 +616,6 @@ 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 From 69e73f7103f0f418c6dc21a6255928268a9e5df8 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 20 Oct 2023 15:25:54 +1100 Subject: [PATCH 5/5] Tidy up changelog --- protocols/relay/CHANGELOG.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index 275536bf406..01e8dc2834d 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,14 +1,7 @@ ## 0.17.0 - unreleased - ## 0.16.2 - - ## 0.16.1 - Export `RateLimiter` type.