Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions prdoc/pr_9251.prdoc
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions substrate/client/cli/src/params/network_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use clap::Args;
use sc_network::{
config::{
NetworkConfiguration, NodeKeyConfig, NonReservedPeerMode, SetConfig, TransportConfig,
DEFAULT_IDLE_CONNECTION_TIMEOUT,
},
multiaddr::Protocol,
};
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions substrate/client/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions substrate/client/network/src/litep2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -519,9 +516,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> 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 {
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ where
// necessary. See <https://github.com/paritytech/substrate/pull/6080>
.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)
};
Expand Down
Loading