Skip to content

Commit f4edafb

Browse files
feat(swarm): set default for idle-connection-timeout to 10s (#4967)
## Description With the move to a global idle-connection-timeout, connections are being closed much more aggressively. This causes problems in situations where, e.g. an application wants to use a connection shortly after an event has been emitted from the `Swarm`. With a default of 0 seconds, such a connection is instantly considered idle and therefore closed, despite the application wanting to use it again just moments later. Whilst it is possible to structure application code to mitigate this, it is unnecessarily complicated. Additionally, connections being closed instantly if not in use is a foot-gun for newcomers to the library. From a technical point-of-view, instantly closing idle connections is nice. In reality, it is an impractical default. Hence, we change this default to 10s. 10 seconds is considered to be an acceptable default as it strikes a balance between allowing some pause between network activity, yet frees up resources that are (supposedly) no longer needed. Resolves: #4912.
1 parent be28c96 commit f4edafb

File tree

26 files changed

+34
-79
lines changed

26 files changed

+34
-79
lines changed

examples/autonat/src/bin/autonat_client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
6262
yamux::Config::default,
6363
)?
6464
.with_behaviour(|key| Behaviour::new(key.public()))?
65-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
6665
.build();
6766

6867
swarm.listen_on(

examples/autonat/src/bin/autonat_server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#![doc = include_str!("../../README.md")]
2222

23-
use std::{error::Error, net::Ipv4Addr, time::Duration};
23+
use std::{error::Error, net::Ipv4Addr};
2424

2525
use clap::Parser;
2626
use futures::StreamExt;
@@ -56,7 +56,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
5656
yamux::Config::default,
5757
)?
5858
.with_behaviour(|key| Behaviour::new(key.public()))?
59-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
6059
.build();
6160

6261
swarm.listen_on(

examples/browser-webrtc/src/main.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![allow(non_upper_case_globals)]
22

3-
use std::{
4-
net::{Ipv4Addr, SocketAddr},
5-
time::Duration,
6-
};
3+
use std::net::{Ipv4Addr, SocketAddr};
74

85
use anyhow::Result;
96
use axum::{
@@ -41,11 +38,6 @@ async fn main() -> anyhow::Result<()> {
4138
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn))))
4239
})?
4340
.with_behaviour(|_| ping::Behaviour::default())?
44-
.with_swarm_config(|cfg| {
45-
cfg.with_idle_connection_timeout(
46-
Duration::from_secs(u64::MAX), // Allows us to observe the pings.
47-
)
48-
})
4941
.build();
5042

5143
let address_webrtc = Multiaddr::from(Ipv4Addr::UNSPECIFIED)

examples/chat/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
8484
mdns::tokio::Behaviour::new(mdns::Config::default(), key.public().to_peer_id())?;
8585
Ok(MyBehaviour { gossipsub, mdns })
8686
})?
87-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
8887
.build();
8988

9089
// Create a Gossipsub topic

examples/dcutr/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#![doc = include_str!("../README.md")]
2222

23-
use std::{error::Error, str::FromStr, time::Duration};
23+
use std::{error::Error, str::FromStr};
2424

2525
use clap::Parser;
2626
use futures::{executor::block_on, future::FutureExt, stream::StreamExt};
@@ -105,7 +105,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
105105
)),
106106
dcutr: dcutr::Behaviour::new(keypair.public().to_peer_id()),
107107
})?
108-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
109108
.build();
110109

111110
swarm

examples/distributed-key-value-store/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#![doc = include_str!("../README.md")]
2222

23-
use std::{error::Error, time::Duration};
23+
use std::error::Error;
2424

2525
use futures::stream::StreamExt;
2626
use libp2p::{
@@ -68,7 +68,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
6868
)?,
6969
})
7070
})?
71-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
7271
.build();
7372

7473
swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server));

examples/identify/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#![doc = include_str!("../README.md")]
2222

23-
use std::{error::Error, time::Duration};
23+
use std::error::Error;
2424

2525
use futures::StreamExt;
2626
use libp2p::{core::multiaddr::Multiaddr, identify, noise, swarm::SwarmEvent, tcp, yamux};
@@ -45,7 +45,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
4545
key.public(),
4646
))
4747
})?
48-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
4948
.build();
5049

5150
// Tell the swarm to listen on all interfaces and a random, OS-assigned

examples/ipfs-kad/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ async fn main() -> Result<()> {
7070
let store = kad::store::MemoryStore::new(key.public().to_peer_id());
7171
kad::Behaviour::with_config(key.public().to_peer_id(), store, cfg)
7272
})?
73-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5)))
7473
.build();
7574

7675
// Add the bootnodes to the local routing table. `libp2p-dns` built

examples/ipfs-private/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#![doc = include_str!("../README.md")]
2222

23-
use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration};
23+
use std::{env, error::Error, fs, path::Path, str::FromStr};
2424

2525
use either::Either;
2626
use futures::prelude::*;
@@ -152,7 +152,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
152152
ping: ping::Behaviour::new(ping::Config::new()),
153153
})
154154
})?
155-
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
156155
.build();
157156

158157
println!("Subscribing to {gossipsub_topic:?}");

examples/metrics/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#![doc = include_str!("../README.md")]
2222

23-
use std::{error::Error, time::Duration};
23+
use std::error::Error;
2424

2525
use futures::StreamExt;
2626
use libp2p::{
@@ -54,7 +54,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
5454
)?
5555
.with_bandwidth_metrics(&mut metric_registry)
5656
.with_behaviour(|key| Behaviour::new(key.public()))?
57-
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)))
5857
.build();
5958

6059
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

0 commit comments

Comments
 (0)