From 79f961fdc150f727a8973b8a33e900f433b88cb0 Mon Sep 17 00:00:00 2001 From: Leonz Date: Fri, 20 Oct 2023 14:08:28 +0800 Subject: [PATCH] feat(request-response): remove `Config::set_connection_keep_alive` This function has been deprecated and can now be removed. Related: #3844. Related: #4678. Pull-Request: #4679. --- protocols/rendezvous/tests/rendezvous.rs | 4 ++-- protocols/request-response/CHANGELOG.md | 2 ++ protocols/request-response/src/handler.rs | 16 ++++------------ protocols/request-response/src/lib.rs | 13 ------------- 4 files changed, 8 insertions(+), 27 deletions(-) diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index 64cd294eefa..67b4bc6ad57 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -370,7 +370,7 @@ async fn registration_on_clients_expire() { let roberts_peer_id = *robert.local_peer_id(); tokio::spawn(robert.loop_on_next()); - let registration_ttl = 3; + let registration_ttl = 1; alice .behaviour_mut() @@ -389,7 +389,7 @@ async fn registration_on_clients_expire() { event => panic!("Unexpected event: {event:?}"), } - tokio::time::sleep(Duration::from_secs(registration_ttl + 5)).await; + tokio::time::sleep(Duration::from_secs(registration_ttl + 1)).await; let event = bob.select_next_some().await; let error = bob.dial(*alice.local_peer_id()).unwrap_err(); diff --git a/protocols/request-response/CHANGELOG.md b/protocols/request-response/CHANGELOG.md index fccc7b2f168..872d1320e7e 100644 --- a/protocols/request-response/CHANGELOG.md +++ b/protocols/request-response/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.26.0 - unreleased +- Remove `request_response::Config::set_connection_keep_alive` in favor of `SwarmBuilder::idle_connection_timeout`. + See [PR 4679](https://github.com/libp2p/rust-libp2p/pull/4679). ## 0.25.2 diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index 86f036365b8..69929b77873 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -27,7 +27,6 @@ use crate::handler::protocol::{RequestProtocol, ResponseProtocol}; use crate::{RequestId, EMPTY_QUEUE_SHRINK_THRESHOLD}; use futures::{channel::oneshot, future::BoxFuture, prelude::*, stream::FuturesUnordered}; -use instant::Instant; use libp2p_swarm::handler::{ ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, ListenUpgradeError, @@ -57,9 +56,6 @@ where inbound_protocols: SmallVec<[TCodec::Protocol; 2]>, /// The request/response message codec. codec: TCodec, - /// The keep-alive timeout of idle connections. A connection is considered - /// idle if there are no outbound substreams. - keep_alive_timeout: Duration, /// The timeout for inbound and outbound substreams (i.e. request /// and response processing). substream_timeout: Duration, @@ -92,7 +88,6 @@ where pub(super) fn new( inbound_protocols: SmallVec<[TCodec::Protocol; 2]>, codec: TCodec, - keep_alive_timeout: Duration, substream_timeout: Duration, inbound_request_id: Arc, ) -> Self { @@ -100,7 +95,6 @@ where inbound_protocols, codec, keep_alive: KeepAlive::Yes, - keep_alive_timeout, substream_timeout, outbound: VecDeque::new(), inbound: FuturesUnordered::new(), @@ -336,13 +330,11 @@ where self.outbound.shrink_to_fit(); } - #[allow(deprecated)] if self.inbound.is_empty() && self.keep_alive.is_yes() { - // No new inbound or outbound requests. However, we may just have - // started the latest inbound or outbound upgrade(s), so make sure - // the keep-alive timeout is preceded by the substream timeout. - let until = Instant::now() + self.substream_timeout + self.keep_alive_timeout; - self.keep_alive = KeepAlive::Until(until); + // 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 diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index f04755ebb0d..9520a0b686d 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -284,28 +284,17 @@ impl fmt::Display for RequestId { #[derive(Debug, Clone)] pub struct Config { request_timeout: Duration, - connection_keep_alive: Duration, } impl Default for Config { fn default() -> Self { Self { - connection_keep_alive: Duration::from_secs(10), request_timeout: Duration::from_secs(10), } } } impl Config { - /// Sets the keep-alive timeout of idle connections. - #[deprecated( - note = "Set a global idle connection timeout via `SwarmBuilder::idle_connection_timeout` instead." - )] - pub fn set_connection_keep_alive(&mut self, v: Duration) -> &mut Self { - self.connection_keep_alive = v; - self - } - /// Sets the timeout for inbound and outbound requests. pub fn set_request_timeout(&mut self, v: Duration) -> &mut Self { self.request_timeout = v; @@ -717,7 +706,6 @@ where self.inbound_protocols.clone(), self.codec.clone(), self.config.request_timeout, - self.config.connection_keep_alive, self.next_inbound_id.clone(), ); @@ -760,7 +748,6 @@ where self.inbound_protocols.clone(), self.codec.clone(), self.config.request_timeout, - self.config.connection_keep_alive, self.next_inbound_id.clone(), );