From 6d3dbcb129b67152c89bbbe2a7dad98b071bf838 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 13:19:32 +0000 Subject: Two options for a node on a machine that should idle --libp2p-ping-interval: how often libp2p pings each open connection, 15 s unless given, 0 for once as it opens. The pings keep no connection open and close none; continuum takes each connection's round trip from them, to keep the fastest of several to one peer. --worker-threads: how many threads take the node's work in turns, as many as the machine has cores unless given. The runtime is built from the configuration, so the count is set as the node starts. Each wakes the node's threads less often; docs/operations.md, An idle node, says what to choose, and what was measured. --- src/config/mod.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 18 ++++++++++++++++-- src/main.rs | 7 +++---- src/simtest/identity.rs | 1 + src/transport/libp2p/behaviour.rs | 16 +++++++++++++++- src/transport/libp2p/config.rs | 4 ++++ src/transport/libp2p/tests.rs | 3 +++ 7 files changed, 70 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/config/mod.rs b/src/config/mod.rs index 1b9a439..6f9bd98 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -503,6 +503,13 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ None, "Longest wait between HELLOs to a member that doesn't answer (dormant, or unanswered HELLOs): they start at 10 keepalives and double up to this. It bounds how long a returning member, or the other side of a partition, takes to be noticed. Default: 60 keepalives (1h at 1m). Shorter than 10 keepalives, it is every wait.", ), + option( + "worker-threads", + Kind::Integer, + "COUNT", + None, + "How many threads take the node's work in turns (at least 1): as many as the machine has cores unless given. What takes long (reading and saving the document, judging a store's events, a remake's snapshot) runs on threads of its own beside them, started as needed. Fewer wake less often on a node that is idle most of the time; set as the node starts, and changed only by a restart (docs/operations.md, Worker threads).", + ), option( "neighbours", Kind::Integer, @@ -636,6 +643,13 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ Some("false"), "With the libp2p transport: find nodes on the LAN (multicast DNS, IPv4). Those that speak continuum are seeds while joining, so nodes on one LAN find each other without configured seeds; a swarm key keeps a new node from joining another swarm on the same LAN.", ), + option( + "libp2p-ping-interval", + Kind::Duration, + "DURATION", + Some("15s"), + "With the libp2p transport: how often each open connection is pinged, for its round trip: of several connections to one peer the fastest is kept, by the shortest round trip each measured. Every connection is pinged once as it opens, whatever this says; 0 leaves it at that. The pings keep no connection open and close none: continuum's own keepalives tell whether a peer answers. A longer interval, or none, spares a node that is idle most of the time the wakeups (docs/transports.md, Pings).", + ), option( "libp2p-upnp", Kind::Flag, @@ -961,6 +975,8 @@ pub(crate) struct Settings { serialize_with = "crate::units::serialize_optional_duration" )] pub(crate) dormant_probe_max: Option, + #[serde(default)] + pub(crate) worker_threads: Option, #[serde(default = "default_neighbours")] pub(crate) neighbours: usize, #[serde( @@ -1012,6 +1028,12 @@ pub(crate) struct Settings { #[serde(default)] #[cfg_attr(not(feature = "libp2p"), allow(dead_code))] pub(crate) libp2p_upnp: bool, + #[serde( + default = "default_libp2p_ping_interval", + with = "crate::units::duration_option" + )] + #[cfg_attr(not(feature = "libp2p"), allow(dead_code))] + pub(crate) libp2p_ping_interval: Duration, #[serde(default)] #[cfg_attr(not(feature = "libp2p"), allow(dead_code))] pub(crate) libp2p_ignore: Vec, @@ -1099,6 +1121,9 @@ fn default_keepalive() -> Duration { fn default_stream_heartbeat() -> Duration { Duration::from_secs(30) } +fn default_libp2p_ping_interval() -> Duration { + Duration::from_secs(15) +} fn default_neighbours() -> usize { 12 } @@ -1284,6 +1309,9 @@ impl Settings { if self.debug_epoch.is_some() && !self.debug_api { bail!("debug-epoch needs debug-api"); } + if self.worker_threads == Some(0) { + bail!("worker-threads must be at least 1"); + } if self.neighbours < 2 { bail!("neighbours must be at least 2"); } diff --git a/src/lib.rs b/src/lib.rs index a420555..8668347 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,8 +77,9 @@ fn voter(args: &config::Settings) -> Result> { })) } -/// Runs a node, as its arguments and configuration say, until it stops. -pub async fn run() -> Result<()> { +/// Runs a node, as its arguments and configuration say, until it stops: on a runtime +/// of as many worker threads as `--worker-threads` says, or as the machine has cores. +pub fn main() -> Result<()> { logging::init(); if let Some((file, create)) = config::asked_node_id() { @@ -98,6 +99,19 @@ pub async fn run() -> Result<()> { ); return Ok(()); } + let mut runtime = tokio::runtime::Builder::new_multi_thread(); + runtime.enable_all(); + if let Some(threads) = config.settings.worker_threads { + runtime.worker_threads(threads); + } + runtime + .build() + .context("Starting the runtime")? + .block_on(run(config)) +} + +/// Runs the node `config` describes, until it stops. +async fn run(config: config::Loaded) -> Result<()> { let args = &config.settings; if let Some(format) = logging::LogFormat::parse(&args.log_format) { logging::set_format(format); diff --git a/src/main.rs b/src/main.rs index 00ab098..9a7e295 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ -//! A continuum node: the library's (`continuum::run`). +//! A continuum node: the library's (`continuum::main`). -#[tokio::main] -async fn main() -> anyhow::Result<()> { - continuum::run().await +fn main() -> anyhow::Result<()> { + continuum::main() } diff --git a/src/simtest/identity.rs b/src/simtest/identity.rs index f1a1b05..0df40f9 100644 --- a/src/simtest/identity.rs +++ b/src/simtest/identity.rs @@ -226,6 +226,7 @@ async fn nodes_over_libp2p() { continuum: true, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), diff --git a/src/transport/libp2p/behaviour.rs b/src/transport/libp2p/behaviour.rs index 0b9be6b..33155d0 100644 --- a/src/transport/libp2p/behaviour.rs +++ b/src/transport/libp2p/behaviour.rs @@ -187,6 +187,8 @@ pub(super) struct Features { pub(super) relay: bool, pub(super) mdns: bool, pub(super) upnp: bool, + /// How often an open connection is pinged, after the ping as it opens. + pub(super) ping_interval: Duration, pub(super) public: bool, /// How long a connection may idle (`idle_timeout`). pub(super) idle_timeout: Duration, @@ -198,12 +200,21 @@ impl Features { relay: config.relay, mdns: config.mdns, upnp: config.upnp, + // None: so long that none comes. A connection's first ping goes as it opens. + ping_interval: if config.ping_interval.is_zero() { + NO_PINGS + } else { + config.ping_interval + }, public: config.public, idle_timeout: idle_timeout(config.keepalive), } } } +/// The interval of pings that never come (`--libp2p-ping-interval 0`): ten years. +const NO_PINGS: Duration = Duration::from_secs(10 * 365 * 24 * 3600); + /// The IPFS DHT's protocol, and its bootstrap nodes (all at `IPFS_BOOTSTRAP_ADDRESS`). pub(super) const IPFS_DHT: StreamProtocol = StreamProtocol::new("/ipfs/kad/1.0.0"); @@ -230,6 +241,7 @@ impl Behaviour { relay, mdns, upnp, + ping_interval, public, idle_timeout: _, } = features; @@ -286,7 +298,9 @@ impl Behaviour { relay_client, relay: relay.into(), dcutr: HolePunching::new(peer_id, ignored), - ping: ::libp2p::ping::Behaviour::new(::libp2p::ping::Config::new()), + ping: ::libp2p::ping::Behaviour::new( + ::libp2p::ping::Config::new().with_interval(ping_interval), + ), autonat: ::libp2p::autonat::Behaviour::new( peer_id, ::libp2p::autonat::Config { diff --git a/src/transport/libp2p/config.rs b/src/transport/libp2p/config.rs index 712f2e2..70a8561 100644 --- a/src/transport/libp2p/config.rs +++ b/src/transport/libp2p/config.rs @@ -21,6 +21,9 @@ pub(crate) struct Libp2pConfig { pub(crate) mdns: bool, /// Ask the router to forward our listen ports (UPnP), and publish where. pub(crate) upnp: bool, + /// How often each open connection is pinged after the ping as it opens; zero for + /// never (`--libp2p-ping-interval`). + pub(crate) ping_interval: std::time::Duration, /// Address ranges neither published nor dialled, nor connected over (`Ignoring`). pub(crate) ignore: Vec, /// Address ranges that matter most, best first (`Book::preference`). @@ -136,6 +139,7 @@ impl Libp2pConfig { continuum: !relay_only, mdns: settings.libp2p_mdns, upnp: settings.libp2p_upnp, + ping_interval: settings.libp2p_ping_interval, ignore: parse_ranges("libp2p-ignore", &settings.libp2p_ignore)?, prefer: parse_ranges("libp2p-prefer", &settings.libp2p_prefer)?, internet_hosts: settings.libp2p_internet_hosts.clone(), diff --git a/src/transport/libp2p/tests.rs b/src/transport/libp2p/tests.rs index 93f251f..7c9f0f0 100644 --- a/src/transport/libp2p/tests.rs +++ b/src/transport/libp2p/tests.rs @@ -41,6 +41,7 @@ fn memory_node_with( continuum: true, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), @@ -231,6 +232,7 @@ async fn announced_addresses_are_published_instead() { continuum: true, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), @@ -473,6 +475,7 @@ async fn a_relay_that_is_no_node() { continuum: false, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), -- cgit v1.3.1