Add jittered Electrum client connection max age - #240
Conversation
There was a problem hiding this comment.
🟡 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_timeoutrather 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.
There was a problem hiding this comment.
🟢 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>
c8df235 to
1135bb6
Compare
What changed
--electrum-rpc-conn-max-age <seconds>flag0Why
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 blockingwrite_allto 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 aWeakreference 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 (blockedwrite_allforced to fail at the deadline), an fd-release test, and a deadline-overflow testcargo check --all-targetsand--features liquid/--features electrum-discoverychecks clean--electrum-rpc-conn-max-agegit diff --check: clean