Skip to content

Add jittered Electrum client connection max age - #240

Open
DeviaVir wants to merge 1 commit into
new-indexfrom
agent/electrum-client-connection-max-age
Open

Add jittered Electrum client connection max age#240
DeviaVir wants to merge 1 commit into
new-indexfrom
agent/electrum-client-connection-max-age

Conversation

@DeviaVir

@DeviaVir DeviaVir commented Aug 3, 2026

Copy link
Copy Markdown

What changed

  • add an opt-in --electrum-rpc-conn-max-age <seconds> flag
  • keep the existing unlimited connection lifetime when the flag is unset or 0
  • assign each accepted client a lifetime jittered between 50% and 100% of the configured maximum
  • enforce each deadline with a single RPC-wide reaper thread that shuts the socket down at the absolute deadline — no timer thread per connection

Why

Electrum clients keep TCP sessions open for long periods. With connection-level L4 balancing, sequential replica restarts can pin most sessions to one backend and saturate it. Bounded, jittered lifetimes let clients reconnect gradually while all backends are available, allowing the load balancer to redistribute sessions without a synchronized reconnect storm.

How the deadline is enforced

The reaper tracks all connection deadlines in a min-heap and calls shutdown() on the connection's socket at the absolute deadline. Shutting the socket down unblocks both peer threads even when the writer is stuck in a blocking write_all to a client that requested a large response and stopped reading — a case an in-band expiry check between messages can never catch. The reaper holds only a Weak reference to each stream, so a connection that ends before its deadline releases its file descriptor immediately.

Validation

  • cargo test --lib: 26 passed, including a non-reading-client test (blocked write_all forced to fail at the deadline), an fd-release test, and a deadline-overflow test
  • cargo check --all-targets and --features liquid / --features electrum-discovery checks clean
  • CLI help verified for --electrum-rpc-conn-max-age
  • git diff --check: clean

@DeviaVir
DeviaVir marked this pull request as ready for review August 4, 2026 10:22
@DeviaVir
DeviaVir requested a lite review from Copilot August 4, 2026 10:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Not ready to approve

It introduces a potential panic via Instant overflow and an expected-expiry path that can cause avoidable warn-level log noise.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

Adds an opt-in, jittered maximum lifetime for inbound Electrum RPC TCP connections to help L4 load balancers redistribute long-lived client sessions gradually and avoid backend pinning.

Changes:

  • Introduces --electrum-rpc-conn-max-age <seconds> config/CLI flag (0/unset keeps unlimited lifetime).
  • Assigns each accepted Electrum client a randomized lifetime (50–100% of configured max) and enforces expiry via recv_timeout rather than per-connection timer threads.
  • Adds unit tests for lifetime selection and expiry behavior.
File summaries
File Description
tests/common.rs Updates test Config construction to include the new Electrum connection max-age field.
src/electrum/server.rs Implements per-connection expiry (jittered lifetime + receive-with-deadline) and adds unit tests.
src/config.rs Adds CLI flag parsing and plumbs electrum_rpc_conn_max_age into runtime configuration.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment thread src/electrum/server.rs Outdated
Comment thread src/electrum/server.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Ready to approve

The new behavior is opt-in (default remains unlimited) and the connection-expiry logic is bounded and covered by unit tests.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Add an opt-in --electrum-rpc-conn-max-age <seconds> flag (default 0 =
unlimited). Each accepted client gets a lifetime jittered between 50%
and 100% of the configured maximum, so clients behind a connection-level
load balancer reconnect gradually instead of in a synchronized storm.

A single RPC-wide reaper thread tracks all connection deadlines in a
min-heap and shuts the socket down at the absolute deadline. The socket
shutdown unblocks both peer threads even when the writer is stuck in a
blocking write to a client that stopped reading, which an in-band expiry
check between messages could never catch. The reaper holds only a Weak
reference to each stream, so connections that end early release their
file descriptor immediately instead of at the deadline.

Signed-off-by: Chase <chase@sillevis.net>
@DeviaVir
DeviaVir force-pushed the agent/electrum-client-connection-max-age branch from c8df235 to 1135bb6 Compare August 4, 2026 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants