diff --git a/examples/autonat/src/bin/autonat_client.rs b/examples/autonat/src/bin/autonat_client.rs index 80d7039eccb..768a2052c80 100644 --- a/examples/autonat/src/bin/autonat_client.rs +++ b/examples/autonat/src/bin/autonat_client.rs @@ -62,7 +62,6 @@ async fn main() -> Result<(), Box> { yamux::Config::default, )? .with_behaviour(|key| Behaviour::new(key.public()))? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm.listen_on( diff --git a/examples/autonat/src/bin/autonat_server.rs b/examples/autonat/src/bin/autonat_server.rs index 83e456d8fda..f3bb6b6a439 100644 --- a/examples/autonat/src/bin/autonat_server.rs +++ b/examples/autonat/src/bin/autonat_server.rs @@ -20,7 +20,7 @@ #![doc = include_str!("../../README.md")] -use std::{error::Error, net::Ipv4Addr, time::Duration}; +use std::{error::Error, net::Ipv4Addr}; use clap::Parser; use futures::StreamExt; @@ -56,7 +56,6 @@ async fn main() -> Result<(), Box> { yamux::Config::default, )? .with_behaviour(|key| Behaviour::new(key.public()))? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm.listen_on( diff --git a/examples/browser-webrtc/src/main.rs b/examples/browser-webrtc/src/main.rs index ec6be0c066d..52222dc882b 100644 --- a/examples/browser-webrtc/src/main.rs +++ b/examples/browser-webrtc/src/main.rs @@ -1,9 +1,6 @@ #![allow(non_upper_case_globals)] -use std::{ - net::{Ipv4Addr, SocketAddr}, - time::Duration, -}; +use std::net::{Ipv4Addr, SocketAddr}; use anyhow::Result; use axum::{ @@ -41,11 +38,6 @@ async fn main() -> anyhow::Result<()> { .map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))) })? .with_behaviour(|_| ping::Behaviour::default())? - .with_swarm_config(|cfg| { - cfg.with_idle_connection_timeout( - Duration::from_secs(u64::MAX), // Allows us to observe the pings. - ) - }) .build(); let address_webrtc = Multiaddr::from(Ipv4Addr::UNSPECIFIED) diff --git a/examples/chat/src/main.rs b/examples/chat/src/main.rs index cda1e90bd35..b0dcc767b6f 100644 --- a/examples/chat/src/main.rs +++ b/examples/chat/src/main.rs @@ -84,7 +84,6 @@ async fn main() -> Result<(), Box> { mdns::tokio::Behaviour::new(mdns::Config::default(), key.public().to_peer_id())?; Ok(MyBehaviour { gossipsub, mdns }) })? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); // Create a Gossipsub topic diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index 0ec1f2a321a..3f403d534e7 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -20,7 +20,7 @@ #![doc = include_str!("../README.md")] -use std::{error::Error, str::FromStr, time::Duration}; +use std::{error::Error, str::FromStr}; use clap::Parser; use futures::{executor::block_on, future::FutureExt, stream::StreamExt}; @@ -105,7 +105,6 @@ async fn main() -> Result<(), Box> { )), dcutr: dcutr::Behaviour::new(keypair.public().to_peer_id()), })? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm diff --git a/examples/distributed-key-value-store/src/main.rs b/examples/distributed-key-value-store/src/main.rs index 63944f2e9bd..3522c84c720 100644 --- a/examples/distributed-key-value-store/src/main.rs +++ b/examples/distributed-key-value-store/src/main.rs @@ -20,7 +20,7 @@ #![doc = include_str!("../README.md")] -use std::{error::Error, time::Duration}; +use std::error::Error; use futures::stream::StreamExt; use libp2p::{ @@ -68,7 +68,6 @@ async fn main() -> Result<(), Box> { )?, }) })? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server)); diff --git a/examples/identify/src/main.rs b/examples/identify/src/main.rs index 55d093c0399..3f08ac01e23 100644 --- a/examples/identify/src/main.rs +++ b/examples/identify/src/main.rs @@ -20,7 +20,7 @@ #![doc = include_str!("../README.md")] -use std::{error::Error, time::Duration}; +use std::error::Error; use futures::StreamExt; use libp2p::{core::multiaddr::Multiaddr, identify, noise, swarm::SwarmEvent, tcp, yamux}; @@ -45,7 +45,6 @@ async fn main() -> Result<(), Box> { key.public(), )) })? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); // Tell the swarm to listen on all interfaces and a random, OS-assigned diff --git a/examples/ipfs-kad/src/main.rs b/examples/ipfs-kad/src/main.rs index c2df603fcc2..8d9a289bdd1 100644 --- a/examples/ipfs-kad/src/main.rs +++ b/examples/ipfs-kad/src/main.rs @@ -70,7 +70,6 @@ async fn main() -> Result<()> { let store = kad::store::MemoryStore::new(key.public().to_peer_id()); kad::Behaviour::with_config(key.public().to_peer_id(), store, cfg) })? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(); // Add the bootnodes to the local routing table. `libp2p-dns` built diff --git a/examples/ipfs-private/src/main.rs b/examples/ipfs-private/src/main.rs index 19d38c767e9..6d8f9beb75d 100644 --- a/examples/ipfs-private/src/main.rs +++ b/examples/ipfs-private/src/main.rs @@ -20,7 +20,7 @@ #![doc = include_str!("../README.md")] -use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration}; +use std::{env, error::Error, fs, path::Path, str::FromStr}; use either::Either; use futures::prelude::*; @@ -152,7 +152,6 @@ async fn main() -> Result<(), Box> { ping: ping::Behaviour::new(ping::Config::new()), }) })? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); println!("Subscribing to {gossipsub_topic:?}"); diff --git a/examples/metrics/src/main.rs b/examples/metrics/src/main.rs index 62e5b06251d..6f6e9d08e31 100644 --- a/examples/metrics/src/main.rs +++ b/examples/metrics/src/main.rs @@ -20,7 +20,7 @@ #![doc = include_str!("../README.md")] -use std::{error::Error, time::Duration}; +use std::error::Error; use futures::StreamExt; use libp2p::{ @@ -54,7 +54,6 @@ async fn main() -> Result<(), Box> { )? .with_bandwidth_metrics(&mut metric_registry) .with_behaviour(|key| Behaviour::new(key.public()))? - .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) .build(); swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; diff --git a/examples/rendezvous/src/bin/rzv-discover.rs b/examples/rendezvous/src/bin/rzv-discover.rs index b133c82d158..bdf9aeafdab 100644 --- a/examples/rendezvous/src/bin/rzv-discover.rs +++ b/examples/rendezvous/src/bin/rzv-discover.rs @@ -53,7 +53,6 @@ async fn main() -> Result<(), Box> { rendezvous: rendezvous::client::Behaviour::new(key.clone()), ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))), })? - .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5))) .build(); swarm.dial(rendezvous_point_address.clone()).unwrap(); diff --git a/examples/rendezvous/src/bin/rzv-identify.rs b/examples/rendezvous/src/bin/rzv-identify.rs index ce4933a29a9..00e94627292 100644 --- a/examples/rendezvous/src/bin/rzv-identify.rs +++ b/examples/rendezvous/src/bin/rzv-identify.rs @@ -56,7 +56,6 @@ async fn main() { ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))), }) .unwrap() - .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5))) .build(); let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()); diff --git a/examples/rendezvous/src/bin/rzv-register.rs b/examples/rendezvous/src/bin/rzv-register.rs index 8ef2d30c880..f70eda5d55e 100644 --- a/examples/rendezvous/src/bin/rzv-register.rs +++ b/examples/rendezvous/src/bin/rzv-register.rs @@ -52,7 +52,6 @@ async fn main() { ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))), }) .unwrap() - .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5))) .build(); // In production the external address should be the publicly facing IP address of the rendezvous diff --git a/examples/rendezvous/src/main.rs b/examples/rendezvous/src/main.rs index 0f26f2c9934..a345d0faed9 100644 --- a/examples/rendezvous/src/main.rs +++ b/examples/rendezvous/src/main.rs @@ -55,7 +55,6 @@ async fn main() -> Result<(), Box> { rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()), ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))), })? - .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5))) .build(); let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/62649".parse().unwrap()); diff --git a/interop-tests/src/arch.rs b/interop-tests/src/arch.rs index 87a508742dc..91fc69dc215 100644 --- a/interop-tests/src/arch.rs +++ b/interop-tests/src/arch.rs @@ -49,7 +49,6 @@ pub(crate) mod native { .with_tokio() .with_quic() .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/udp/0/quic-v1"), ), @@ -62,7 +61,6 @@ pub(crate) mod native { mplex::MplexConfig::default, )? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0"), ), @@ -75,7 +73,6 @@ pub(crate) mod native { yamux::Config::default, )? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0"), ), @@ -88,7 +85,6 @@ pub(crate) mod native { mplex::MplexConfig::default, )? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0"), ), @@ -101,7 +97,6 @@ pub(crate) mod native { yamux::Config::default, )? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0"), ), @@ -111,7 +106,6 @@ pub(crate) mod native { .with_websocket(tls::Config::new, mplex::MplexConfig::default) .await? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0/ws"), ), @@ -121,7 +115,6 @@ pub(crate) mod native { .with_websocket(tls::Config::new, yamux::Config::default) .await? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0/ws"), ), @@ -131,7 +124,6 @@ pub(crate) mod native { .with_websocket(noise::Config::new, mplex::MplexConfig::default) .await? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0/ws"), ), @@ -141,7 +133,6 @@ pub(crate) mod native { .with_websocket(noise::Config::new, yamux::Config::default) .await? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/tcp/0/ws"), ), @@ -155,7 +146,6 @@ pub(crate) mod native { )) })? .with_behaviour(behaviour_constructor)? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) .build(), format!("/ip4/{ip}/udp/0/webrtc-direct"), ), diff --git a/libp2p/CHANGELOG.md b/libp2p/CHANGELOG.md index e86d633b5a7..5fc21d366bc 100644 --- a/libp2p/CHANGELOG.md +++ b/libp2p/CHANGELOG.md @@ -2,6 +2,8 @@ - Deprecate `void` crate. See [PR 5676](https://github.com/libp2p/rust-libp2p/pull/5676). +- Update default for idle-connection-timeout to 10s. + See [PR 4967](https://github.com/libp2p/rust-libp2p/pull/4967). - Expose swarm builder phase errors. See [PR 5726](https://github.com/libp2p/rust-libp2p/pull/5726). diff --git a/libp2p/Cargo.toml b/libp2p/Cargo.toml index 79f4b8fbb9a..3d44e0bc43c 100644 --- a/libp2p/Cargo.toml +++ b/libp2p/Cargo.toml @@ -142,7 +142,6 @@ clap = { version = "4.1.6", features = ["derive"] } tokio = { workspace = true, features = [ "io-util", "io-std", "macros", "rt", "rt-multi-thread"] } libp2p-mplex = { workspace = true } -libp2p-noise = { workspace = true } libp2p-tcp = { workspace = true, features = ["tokio"] } tracing-subscriber = { workspace = true, features = ["env-filter"] } diff --git a/libp2p/src/tutorials/ping.rs b/libp2p/src/tutorials/ping.rs index f35fef8f488..ebaea29f33a 100644 --- a/libp2p/src/tutorials/ping.rs +++ b/libp2p/src/tutorials/ping.rs @@ -232,9 +232,6 @@ //! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| { -//! cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)) -//! }) //! .build(); //! //! Ok(()) @@ -287,9 +284,6 @@ //! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| { -//! cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)) -//! }) //! .build(); //! //! // Tell the swarm to listen on all interfaces and a random, OS-assigned @@ -335,9 +329,6 @@ //! yamux::Config::default, //! )? //! .with_behaviour(|_| ping::Behaviour::default())? -//! .with_swarm_config(|cfg| { -//! cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)) -//! }) //! .build(); //! //! // Tell the swarm to listen on all interfaces and a random, OS-assigned diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 82749ffb5fd..bf7af0128f0 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -64,8 +64,7 @@ fn build_node_with_config(cfg: Config) -> (Multiaddr, TestSwarm) { transport, behaviour, local_id, - swarm::Config::with_async_std_executor() - .with_idle_connection_timeout(Duration::from_secs(5)), + swarm::Config::with_async_std_executor(), ); let address: Multiaddr = Protocol::Memory(random::()).into(); diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index 125f0dbb4ad..3181e60db74 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -416,10 +416,7 @@ fn reuse_connection() { .with(Protocol::P2p(relay_peer_id)) .with(Protocol::P2pCircuit); - // To reuse the connection, we need to ensure it is not shut down due to being idle. - let mut client = build_client_with_config( - Config::with_async_std_executor().with_idle_connection_timeout(Duration::from_secs(1)), - ); + let mut client = build_client(); let client_peer_id = *client.local_peer_id(); client.dial(relay_addr).unwrap(); diff --git a/swarm-test/CHANGELOG.md b/swarm-test/CHANGELOG.md index 5700460b3a6..1fd213e12f6 100644 --- a/swarm-test/CHANGELOG.md +++ b/swarm-test/CHANGELOG.md @@ -2,6 +2,8 @@ - Add `tokio` runtime support and make `tokio` and `async-std` runtimes optional behind features. See [PR 5551]. + - Update default for idle-connection-timeout to 10s on `SwarmExt::new_ephemeral` methods. + See [PR 4967](https://github.com/libp2p/rust-libp2p/pull/4967). [PR 5551]: https://github.com/libp2p/rust-libp2p/pull/5551 diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 0edf02473e6..0bc417dd8b1 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -246,13 +246,7 @@ where transport, behaviour_fn(identity), peer_id, - libp2p_swarm::Config::with_async_std_executor() - // Some tests need - // connections to be kept - // alive beyond what the - // individual behaviour - // configures., - .with_idle_connection_timeout(Duration::from_secs(5)), + libp2p_swarm::Config::with_async_std_executor(), ) } @@ -279,12 +273,7 @@ where transport, behaviour_fn(identity), peer_id, - libp2p_swarm::Config::with_tokio_executor() - .with_idle_connection_timeout(Duration::from_secs(5)), /* Some tests need - * connections to be kept - * alive beyond what the - * individual behaviour - * configures., */ + libp2p_swarm::Config::with_tokio_executor(), ) } diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 69446e62d07..f1ad994bd1b 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -5,10 +5,13 @@ - Deprecate `void` crate. See [PR 5676](https://github.com/libp2p/rust-libp2p/pull/5676). - + - Fix `cargo clippy` warnings in `rustc 1.84.0-beta.1`. See [PR 5700](https://github.com/libp2p/rust-libp2p/pull/5700). +- Update default for idle-connection-timeout to 10s. + See [PR 4967](https://github.com/libp2p/rust-libp2p/pull/4967). + ## 0.45.1 - Update `libp2p-swarm-derive` to version `0.35.0`, see [PR 5545] diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index f42fd1f305c..49cc774b2b7 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -989,7 +989,7 @@ impl PoolConfig { task_command_buffer_size: 32, per_connection_event_buffer_size: 7, dial_concurrency_factor: NonZeroU8::new(8).expect("8 > 0"), - idle_connection_timeout: Duration::ZERO, + idle_connection_timeout: Duration::from_secs(10), substream_upgrade_protocol_override: None, max_negotiating_inbound_streams: 128, } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 639906a1a09..a20d58b97fc 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1495,7 +1495,18 @@ impl Config { /// How long to keep a connection alive once it is idling. /// - /// Defaults to 0. + /// Defaults to 10s. + /// + /// Typically, you shouldn't _need_ to modify this default as connections will be kept alive whilst they are "in use" (see below). + /// Depending on the application's usecase, it may be desirable to keep connections alive despite them not being in use. + /// + /// A connection is considered idle if: + /// - There are no active inbound streams. + /// - There are no active outbounds streams. + /// - There are no pending outbound streams (i.e. all streams requested via [`ConnectionHandlerEvent::OutboundSubstreamRequest`] have completed). + /// - Every [`ConnectionHandler`] returns `false` from [`ConnectionHandler::connection_keep_alive`]. + /// + /// Once all these conditions are true, the idle connection timeout starts ticking. pub fn with_idle_connection_timeout(mut self, timeout: Duration) -> Self { self.pool_config.idle_connection_timeout = timeout; self @@ -1784,12 +1795,7 @@ mod tests { .boxed(); let behaviour = CallTraceBehaviour::new(MockBehaviour::new(dummy::ConnectionHandler)); - Swarm::new( - transport, - behaviour, - local_public_key.into(), - config.with_idle_connection_timeout(Duration::from_secs(5)), - ) + Swarm::new(transport, behaviour, local_public_key.into(), config) } fn swarms_connected( diff --git a/transports/tls/tests/smoke.rs b/transports/tls/tests/smoke.rs index cf11f4c0b1d..e335f68a7e4 100644 --- a/transports/tls/tests/smoke.rs +++ b/transports/tls/tests/smoke.rs @@ -1,5 +1,3 @@ -use std::time::Duration; - use futures::{future, StreamExt}; use libp2p_core::{multiaddr::Protocol, transport::MemoryTransport, upgrade::Version, Transport}; use libp2p_swarm::{dummy, Config, Swarm, SwarmEvent}; @@ -67,6 +65,6 @@ fn make_swarm() -> Swarm { transport, dummy::Behaviour, identity.public().to_peer_id(), - Config::with_tokio_executor().with_idle_connection_timeout(Duration::from_secs(60)), + Config::with_tokio_executor(), ) }