Skip to content

Commit c795602

Browse files
authored
Merge pull request #225 from Blockstream/daemon-conn-max-age
daemon: configurable max-age to recycle RPC connections
2 parents d7c2d33 + 48418ca commit c795602

5 files changed

Lines changed: 246 additions & 30 deletions

File tree

src/bin/electrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn run_server(config: Arc<Config>, salt_rwlock: Arc<RwLock<String>>) -> Result<(
7474
config.network_type,
7575
signal.clone(),
7676
&metrics,
77+
config.daemon_conn_max_age,
7778
)?);
7879
info!("opening database at {}", config.db_path.display());
7980
let store = Arc::new(Store::open(&config, &metrics, true));

src/bin/tx-fingerprint-stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn main() {
4040
config.network_type,
4141
signal,
4242
&metrics,
43+
config.daemon_conn_max_age,
4344
)
4445
.unwrap(),
4546
);

src/config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::net::SocketAddr;
55
use std::net::ToSocketAddrs;
66
use std::path::{Path, PathBuf};
77
use std::sync::Arc;
8+
use std::time::Duration;
89
use stderrlog;
910

1011
use crate::chain::Network;
@@ -27,6 +28,7 @@ pub struct Config {
2728
pub daemon_rpc_addr: SocketAddr,
2829
pub daemon_rpc_fallback_addr: Option<SocketAddr>,
2930
pub daemon_parallelism: usize,
31+
pub daemon_conn_max_age: Option<Duration>,
3032
pub cookie: Option<String>,
3133
pub electrum_rpc_addr: SocketAddr,
3234
pub http_addr: SocketAddr,
@@ -177,6 +179,13 @@ impl Config {
177179
.help("Number of JSONRPC requests to send in parallel")
178180
.default_value("4")
179181
)
182+
.arg(
183+
Arg::with_name("daemon_rpc_conn_max_age")
184+
.long("daemon-rpc-conn-max-age")
185+
.help("Max age (in seconds) of a daemon RPC TCP connection before it is proactively recycled. Recycling re-establishes the connection, letting a load balancer (e.g. a Kubernetes ClusterSetIP) re-select a backend after node rotations. The reconnect happens inline on the next request, so prefer a generous value (minutes, not seconds) to avoid periodic latency spikes. 0 = unlimited / never recycle (default)")
186+
.default_value("0")
187+
.takes_value(true),
188+
)
180189
.arg(
181190
Arg::with_name("monitoring_addr")
182191
.long("monitoring-addr")
@@ -425,6 +434,12 @@ impl Config {
425434
.value_of("daemon_rpc_fallback_addr")
426435
.map(|e| str_to_socketaddr(e, "Bitcoin Fallback RPC"));
427436

437+
let daemon_conn_max_age: Option<Duration> =
438+
match value_t_or_exit!(m, "daemon_rpc_conn_max_age", u64) {
439+
0 => None, // 0 = unlimited / never recycle
440+
secs => Some(Duration::from_secs(secs)),
441+
};
442+
428443
let electrum_rpc_addr: SocketAddr = str_to_socketaddr(
429444
m.value_of("electrum_rpc_addr")
430445
.unwrap_or(&format!("127.0.0.1:{}", default_electrum_port)),
@@ -494,6 +509,7 @@ impl Config {
494509
daemon_rpc_addr,
495510
daemon_rpc_fallback_addr,
496511
daemon_parallelism: value_t_or_exit!(m, "daemon_parallelism", usize),
512+
daemon_conn_max_age,
497513
cookie,
498514
utxos_limit: value_t_or_exit!(m, "utxos_limit", usize),
499515
electrum_rpc_addr,

0 commit comments

Comments
 (0)