Skip to content

feat(node): warm per-peer connections for DHT RPCs and pull legs - #2475

Merged
thiras merged 10 commits into
mainfrom
feat/warm-peer-connections
Oct 19, 2026
Merged

thiras merged 10 commits into
mainfrom
feat/warm-peer-connections

Conversation

@alpergundogdu

@alpergundogdu alpergundogdu commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A node now keeps one live QUIC connection per peer per ALPN for its outbound cdn/dht/v1 RPCs and cdn/client/v1 pull legs. It no longer dials and closes a connection per request.

Fleet baseline (Grafana, 7-day average): about 35k connection opens per day. DHT BatchStore is about 2/3 of those, and node pulls cost about 2.2 opens each (#2334, #2336). Each fresh iroh connection starts on the relay (200-330 ms RTT against about 30 ms direct) and needs about 0.9 hole-punch attempts.

  • decdn-client: a Connections map now carries its ALPN and an optional dial runtime (with_options, alpn, get_or_dial; all #[doc(hidden)], so the public-API snapshot does not change). With a dial runtime, every connection's QUIC driver lives on that runtime. That makes the map safe to share across the node's throwaway per-serve pull runtimes, and Pull legs run on ephemeral per-serve runtimes, stranding shared connection drivers (HTTP + QUIC) #1675 stays fixed. get_or_dial returns a Lease (Dialled or Pinned) that carries the retry rule. A pooled dial runs as its own task under a root pooled_connection span that follows from the caller's span; dial hands the span to the task rather than entering it on the caller, so the driver inherits no caller span at any log level (a disabled debug span entered on the caller would have left the caller's INFO span current, and the driver would have held it open for the connection's life).
  • Pull legs: a leg whose pinned connection is closed under the open (the upstream's 30 s idle reaper, or its stream budget, between legs) redials once and opens again. Nothing is paid before the header. A fresh dial that fails is the upstream's failure and is not retried. Without this, the race scored an honest upstream Unreachable.
  • DHT client: find_node, store, batch_store and find_value take the node's cdn/dht/v1 map. They reuse the warm connection and never close it.
    • If a pinned connection turns out closed, the call redials once, logs the close reason at debug, and a failure after the redial carries the pinned failure as context.
    • If the call dialled the connection itself and it fails, there is no retry.
    • On a timeout the call unpins the connection it used. A call its caller aborts mid-flight (a lookup round deadline, which fires at the same 8 s as the client timeout) unpins it too, through an UnpinOnDrop guard.
  • DHT server: waits 5 s for a connection's first stream, then 30 s between streams. The 30 s matches the cdn/client/v1 idle reaper (APP_IDLE_TIMEOUT). An elapsed window closes the connection with reason dht-idle, so the requester's next call meets a closed pin and redials at once instead of opening a stream nobody accepts. A peer or transport close logs its error at debug. The 256-request and 5-minute caps are unchanged.
  • Node wiring: the node builds two maps on the main runtime. The DHT map is shared by bootstrap, bucket refresh, republish and lookups. The client map lives in NodeOriginDeps::connections. Every pull-leg PeerSource, the first leg and the header handshake open a stream on the upstream's shared connection. NodeOriginDeps::dial_runtime is gone because no node dial runs outside a map. The header handshake uses the new PeerSource::open_span.
  • Docs: ADR 022 § ALPN and Transport describes the kept connection, the windows and the redial; its look-ahead rationale no longer cites connection setup. CHANGELOG.md gains the client (BREAKING) and node entries, and the decdn-node hangs in router.shutdown() on SIGTERM after any node-to-node pull; SIGKILL skips redeem and lane flush #2185 entry names the shape that exists.

Open decisions (reviewer input wanted)

Both of these are kept as-is in this PR and filed as issues so they are decided, not deferred by default.

  1. Dispatch permits (node: idle warm connections hold dispatch permits for 30 s and can fill max_concurrent_handlers at scale #2541). One permit per connection, as before, but a warm connection now holds its permit for up to 30 s idle (plus the 3 s close flush on the DHT side). The limiter is shared by probe, DHT and client handlers with a default cap of 256. Each peer pair holds about two idle permits in steady state, so idle node-to-node connections alone can fill the cap at roughly 128 active nodes and shed paying clients with global-full. Options: acquire the DHT permit per stream rather than per connection, give idle warm connections their own small pool, or have the requester close a DHT connection after a shorter idle of its own. Watch decdn_dispatch_in_flight after deploy either way.
  2. Shared receive window (node: concurrent pull legs to one upstream share one 8 MiB receive window and the 100-stream cap #2542). Every concurrent leg to one upstream now shares one connection's 8 MiB receive window (QUIC_CONNECTION_RECEIVE_WINDOW) and its 100-stream cap. Aggregate pull throughput per upstream is bounded at about 8 MiB per RTT, and past 100 in-flight legs open_bi waits until the open deadline, which unpins the shared connection for every leg. Options: a larger window on dialled connections, or a small per-upstream connection count.

Tests

  • connection/tests.rs:
    • map_dials_its_alpn, map_with_dial_runtime_survives_caller_runtime_drop, concurrent_gets_share_one_dial.
    • a_pooled_dial_holds_no_caller_span_open_at_info: an INFO-filtered subscriber sees the caller's span close while the pooled connection stays open. Fails against the inline dial.
    • a_leg_on_a_pinned_connection_closed_under_the_open_redials_once and a_leg_on_a_fresh_connection_that_fails_is_not_retried.
    • The existing a_leg_that_times_out_unpins_its_connection covers an open timeout on a connection that never answers.
  • dht_loopback.rs (pooled_client, against the real DhtHandler where the server is not scripted):
    • sequential_rpcs_to_one_peer_dial_once, rpcs_past_the_first_stream_window_ride_one_connection (6 s apart, one dial).
    • pooled_call_redials_after_server_closed_connection, a_fresh_dial_that_fails_is_not_retried, a_failed_redial_is_not_retried (two dials, never three; the third call recovers).
    • dead_peer_fails_within_client_timeout_without_retry, a_call_that_times_out_unpins_its_connection, an_aborted_call_unpins_its_connection.
  • handlers/dht/tests.rs: accept_timeout_is_short_for_first_stream_and_idle_window_after, idle_window_never_exceeds_client_reaper.
  • Anvil e2e warm_upstream_connection.rs: after a warm-up miss, two more misses open no connection on the provider beyond their probes. Each miss probes the provider once on a one-shot cdn/probe/v1 connection (the positive probe cache is keyed by hash), so the test subtracts the provider's inbound decdn_probe_requests_total from its connection count and requires the rest to be zero; a single un-pooled ALPN costs one dial per miss on top and fails. The server then stops on SIGTERM within 30 s with a clean router close.
  • Fixture change in node_origin_pull.rs: the fixture upstreams serve every stream a connection carries through serve_each_stream, as ClientHandler does, and a stream the fixture fails to serve is named in the captured log. Counters that counted connections now count streams. spawn_a_serving_server_with_counters is removed, and its test counts streams from the handler's metrics.

Testnet check after deploy (24 h against the 7-day baseline, grafanacloud-prom)

Query Baseline Expect
sum(increase(decdn_iroh_socket_num_conns_opened_total[1d])) ~35k/day Large drop (DHT BatchStore is ~2/3)
sum(increase(decdn_iroh_socket_holepunch_attempts_total[1d])) / sum(increase(decdn_iroh_socket_num_conns_opened_total[1d])) ~0.9 Roughly flat per connection, far fewer connections
histogram_quantile(0.5, sum by (le) (increase(decdn_node_pull_first_byte_seconds_bucket[1d]))) (and 0.95) p50 1.39 s, p95 2.4 s Lower
sum(rate(decdn_client_idle_close_total[1h])) on upstream nodes n/a Rises: warm connections reaped at 30 s, as designed
max_over_time(decdn_dispatch_in_flight[1d]) n/a Headroom under 256

Then comment the before/after numbers on #2334 and #2336.

🤖 Generated with Claude Code

alpergundogdu and others added 5 commits October 16, 2026 11:02
find_node, store, batch_store and find_value take the node's cdn/dht/v1
Connections map (dialled on the main runtime) and leave the connection
open. A pinned connection found closed redials once; a fresh dial that
fails is not retried.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ndow

The accept loop waits 5 s for a connection's first stream and 30 s
(the cdn/client/v1 idle reaper) between later streams, so a requester's
pooled DHT connection stays warm across bursts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Connections map's connection outlives the caller that dials it, and its
driver keeps the dial's span open. The map dials under a root
pooled_connection span that follows from the caller's, so the caller's
span closes when the caller ends. PeerSource::open_span opens a raw
unaligned range on the source's connection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NodeOriginDeps carries a cdn/client/v1 Connections map dialled on the main
runtime; every pull leg, first leg and header handshake opens a stream on
the upstream's shared connection. dial_runtime is gone: no node dial runs
outside a map. Node-origin fixture upstreams serve every stream a
connection carries, as ClientHandler does. A new anvil e2e test shows two
misses ride the warm connections and shutdown stays prompt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 16, 2026 11:25
@github-code-quality

github-code-quality Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main 22091d2 feat/warm-peer-conne... 78ddd90 +/-
76% 76% 0%
View coverage of the 10 most impacted files.
File main 22091d2 feat/warm-peer-conne... 78ddd90 +/-
crates/node/src...handlers/dht.rs 80% 79% -1%
crates/client/s...c/connection.rs 98% 97% -1%
crates/client/src/lib.rs 89% 89% 0%
crates/client/s...undle/engine.rs 65% 65% 0%
crates/node/src/runtime/mod.rs 26% 26% 0%
crates/client/src/source.rs 93% 93% 0%
crates/node/src...gin/pull_leg.rs 84% 84% 0%
crates/node/src...e_origin/mod.rs 88% 88% 0%
crates/protocol/src/framing.rs 92% 93% +1%
crates/node/src/dht/client.rs 79% 84% +5%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated October 19, 2026 16:10 UTC

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.

🔵 Needs a closer look

It reshapes per-peer connection lifecycle across the DHT client/server and every node pull leg with new redial semantics and a longer dispatch-permit hold window that the author flags for post-deploy monitoring, warranting human review.

1 open finding
What changed in this PR

This PR makes a node keep one warm QUIC connection per peer per ALPN for its outbound cdn/dht/v1 RPCs and cdn/client/v1 pull legs, instead of dialling and closing a connection per request. It targets the ~35k/day fleet connection-open rate (DHT BatchStore ~2/3, node pulls ~2.2 opens/run), where each fresh iroh connection pays a relay round-trip plus a hole-punch. The decdn-client Connections map gains an ALPN and an optional dial runtime so it can be safely shared across the node's throwaway per-serve pull runtimes (fixing #1675), the DHT client reuses the warm connection with a redial-once-on-close policy, and the DHT server holds a connection open for a 30 s idle window (matching the cdn/client/v1 reaper) between streams.

Changes:

  • Connections now carries its ALPN + an optional dial runtime (with_options/alpn/get_or_dial), dialling under a root pooled_connection span; dial takes the ALPN explicitly.
  • DHT client/server + node wiring switched from per-request dials to pooled maps (dht_connections, connections), with redial-once semantics and a first-stream-vs-idle accept window; NodeOriginDeps::dial_runtime is removed.
  • Test fixtures reworked to serve every stream a pooled connection carries (serve_each_stream), plus new unit/loopback/e2e coverage for pooling, redial, and warm-shutdown.
File Description
crates/​client/​src/​connection.rs Connections gains ALPN + dial runtime, get_or_dial, pooled-connection span; dial takes ALPN
crates/​client/​src/​source.rs New open_span for header-only opens; doc updates for the map dial runtime
crates/​client/​src/​lib.rs Passes ALPN_CLIENT to connection::dial
crates/​client/​src/​connection/​tests.rs New tests for ALPN dial, runtime-drop survival, shared dial
crates/​node/​src/​dht/​client.rs Pooled connection + redial-once + unpin-on-timeout exchange logic
crates/​node/​src/​dht/​{bootstrap,bucket_refresh,publish,lookup}.rs Thread Connections through DHT primitives
crates/​node/​src/​handlers/​dht.rs First-stream (5s) vs idle (30s) accept window via accept_timeout
crates/​node/​src/​handlers/​dht/​tests.rs Tests for accept-window split and reaper bound
crates/​node/​src/​handlers/​client/​mod.rs APP_IDLE_TIMEOUT made pub(crate)
crates/​node/​src/​node_origin/​{mod,pull_leg}.rs Pull legs open streams on shared connections; drop dial_runtime
crates/​node/​src/​runtime/​mod.rs Build DHT + client maps on the main runtime; wire into deps
crates/​node/​tests/​* Fixtures serve every stream; DHT tests pass Connections; new pooled-client loopback tests
crates/​e2e/​tests/​warm_upstream_connection.rs New anvil e2e: two warm misses + clean SIGTERM shutdown

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/client/src/source.rs Resolved
alpergundogdu and others added 5 commits October 16, 2026 12:46
Every node dial goes through a Connections map with a dial runtime, so
PeerSource::with_dial_runtime and open_progressive_pull's dial_runtime
parameter have no callers. A map-less open dials on the caller's runtime.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nections

# Conflicts:
#	crates/node/tests/node_origin_pull.rs
…nections

# Conflicts:
#	crates/node/src/node_origin/pull_leg.rs
…nections

# Conflicts:
#	crates/node/src/dht/client.rs
#	crates/node/src/dht/lookup.rs
…er a pull open, and unpin on abort

The pooled dial's root span is a debug span, which the node's INFO
subscriber disables. Entering it on the caller changed nothing, so the
dial task saw the caller's span as current and the connection's driver
held that span open for the connection's life. `dial` now always runs
the connect as a task and hands it the span, so the driver inherits no
caller span at any level.

`Connections::get_or_dial` returns a `Lease` (`Dialled` or `Pinned`)
instead of a bool, so the redial-once rule lives in a match. A pull leg
whose pinned connection is closed under the open redials once, the way
the DHT client does; before, the race with the upstream's idle reaper
scored an honest upstream `Unreachable`. A DHT call that its caller
aborts mid-flight (a lookup round deadline, which fires at the same 8 s
as the client timeout) unpins its connection through an `UnpinOnDrop`
guard. The redial logs the close reason, and a failure after a redial
carries the pinned failure as context.

The DHT server closes an idle connection with reason `dht-idle` instead
of waiting 3 s for a close the requester never sends, and logs a peer or
transport close.

The warm-upstream e2e attributes the provider's new connections: it
subtracts the inbound probes (one per new-hash miss, the positive probe
cache is keyed by hash) and requires the rest to be zero, so a single
un-pooled ALPN fails it. New tests pin the span at INFO, the pull-leg redial and no-retry
legs, the DHT no-retry, timeout-unpin and abort-unpin legs, and the 30 s
idle window against the real handler. ADR 022 describes the kept
connection; the CHANGELOG gains the client and node entries.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@thiras
thiras merged commit 63464db into main Oct 19, 2026
31 checks passed
@thiras
thiras deleted the feat/warm-peer-connections branch October 19, 2026 16:30
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.

3 participants