diff --git a/prdoc/pr_9251.prdoc b/prdoc/pr_9251.prdoc new file mode 100644 index 0000000000000..03f12332d1542 --- /dev/null +++ b/prdoc/pr_9251.prdoc @@ -0,0 +1,10 @@ +title: Allow setting idle connection timeout value in custom node implementations +doc: +- audience: Node Dev + description: Allow setting idle connection timeout value. This can be helpful in + custom networks to allow maintaining long-lived connections. +crates: +- name: sc-cli + bump: major +- name: sc-network + bump: major diff --git a/substrate/client/cli/src/params/network_params.rs b/substrate/client/cli/src/params/network_params.rs index cc6c5ca1f4ff4..57be3de3f0e47 100644 --- a/substrate/client/cli/src/params/network_params.rs +++ b/substrate/client/cli/src/params/network_params.rs @@ -24,6 +24,7 @@ use clap::Args; use sc_network::{ config::{ NetworkConfiguration, NodeKeyConfig, NonReservedPeerMode, SetConfig, TransportConfig, + DEFAULT_IDLE_CONNECTION_TIMEOUT, }, multiaddr::Protocol, }; @@ -273,6 +274,7 @@ impl NetworkParams { enable_mdns: !is_dev && !self.no_mdns, allow_private_ip, }, + idle_connection_timeout: DEFAULT_IDLE_CONNECTION_TIMEOUT, max_parallel_downloads: self.max_parallel_downloads, max_blocks_per_request: self.max_blocks_per_request, min_peers_to_start_warp_sync: None, diff --git a/substrate/client/network/src/config.rs b/substrate/client/network/src/config.rs index 89e5acd6810ce..5f90a7bfe15ee 100644 --- a/substrate/client/network/src/config.rs +++ b/substrate/client/network/src/config.rs @@ -66,8 +66,14 @@ use std::{ pin::Pin, str::{self, FromStr}, sync::Arc, + time::Duration, }; +/// Default timeout for idle connections of 10 seconds is good enough for most networks. +/// It doesn't make sense to expose it as a CLI parameter on individual nodes, but customizations +/// are possible in custom nodes through [`NetworkConfiguration`]. +pub const DEFAULT_IDLE_CONNECTION_TIMEOUT: Duration = Duration::from_secs(10); + /// Protocol name prefix, transmitted on the wire for legacy protocol names. /// I.e., `dot` in `/dot/sync/2`. Should be unique for each chain. Always UTF-8. /// Deprecated in favour of genesis hash & fork ID based protocol names. @@ -620,6 +626,11 @@ pub struct NetworkConfiguration { /// Configuration for the transport layer. pub transport: TransportConfig, + /// Idle connection timeout. + /// + /// Set by default to [`DEFAULT_IDLE_CONNECTION_TIMEOUT`]. + pub idle_connection_timeout: Duration, + /// Maximum number of peers to ask the same blocks in parallel. pub max_parallel_downloads: u32, @@ -677,6 +688,7 @@ impl NetworkConfiguration { client_version: client_version.into(), node_name: node_name.into(), transport: TransportConfig::Normal { enable_mdns: false, allow_private_ip: true }, + idle_connection_timeout: DEFAULT_IDLE_CONNECTION_TIMEOUT, max_parallel_downloads: 5, max_blocks_per_request: 64, min_peers_to_start_warp_sync: None, diff --git a/substrate/client/network/src/litep2p/mod.rs b/substrate/client/network/src/litep2p/mod.rs index 8af076678961c..d1801475fcbbe 100644 --- a/substrate/client/network/src/litep2p/mod.rs +++ b/substrate/client/network/src/litep2p/mod.rs @@ -99,9 +99,6 @@ mod peerstore; mod service; mod shim; -/// Timeout for connection waiting new substreams. -const KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(10); - /// Litep2p bandwidth sink. struct Litep2pBandwidthSink { sink: litep2p::BandwidthSink, @@ -519,9 +516,7 @@ impl NetworkBackend for Litep2pNetworkBac .with_connection_limits(ConnectionLimitsConfig::default().max_incoming_connections( Some(crate::MAX_CONNECTIONS_ESTABLISHED_INCOMING as usize), )) - // This has the same effect as `libp2p::Swarm::with_idle_connection_timeout` which is - // set to 10 seconds as well. - .with_keep_alive_timeout(KEEP_ALIVE_TIMEOUT) + .with_keep_alive_timeout(network_config.idle_connection_timeout) .with_executor(executor); if let Some(config) = maybe_mdns_config { diff --git a/substrate/client/network/src/service.rs b/substrate/client/network/src/service.rs index 0e755a39161c0..19c7e8ee323b1 100644 --- a/substrate/client/network/src/service.rs +++ b/substrate/client/network/src/service.rs @@ -549,7 +549,7 @@ where // necessary. See .with_per_connection_event_buffer_size(24) .with_max_negotiating_inbound_streams(2048) - .with_idle_connection_timeout(Duration::from_secs(10)); + .with_idle_connection_timeout(network_config.idle_connection_timeout); Swarm::new(transport, behaviour, local_peer_id, config) };