Repository navigation
feat(node): warm per-peer connections for DHT RPCs and pull legs - #2475
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
Code coverage overviewLanguages: Rust Rust / code-coverage/cargo-llvm-cov
View coverage of the 10 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. Updated |
There was a problem hiding this comment.
🔵 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:
Connectionsnow carries its ALPN + an optional dial runtime (with_options/alpn/get_or_dial), dialling under a rootpooled_connectionspan;dialtakes 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_runtimeis 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.
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]>

Summary
A node now keeps one live QUIC connection per peer per ALPN for its outbound
cdn/dht/v1RPCs andcdn/client/v1pull legs. It no longer dials and closes a connection per request.Fleet baseline (Grafana, 7-day average): about 35k connection opens per day. DHT
BatchStoreis 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: aConnectionsmap 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_dialreturns aLease(DialledorPinned) that carries the retry rule. A pooled dial runs as its own task under a rootpooled_connectionspan that follows from the caller's span;dialhands 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).Unreachable.find_node,store,batch_storeandfind_valuetake the node'scdn/dht/v1map. They reuse the warm connection and never close it.UnpinOnDropguard.cdn/client/v1idle reaper (APP_IDLE_TIMEOUT). An elapsed window closes the connection with reasondht-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.NodeOriginDeps::connections. Every pull-legPeerSource, the first leg and the header handshake open a stream on the upstream's shared connection.NodeOriginDeps::dial_runtimeis gone because no node dial runs outside a map. The header handshake uses the newPeerSource::open_span.CHANGELOG.mdgains 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.
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. Watchdecdn_dispatch_in_flightafter deploy either way.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 legsopen_biwaits 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_onceanda_leg_on_a_fresh_connection_that_fails_is_not_retried.a_leg_that_times_out_unpins_its_connectioncovers an open timeout on a connection that never answers.dht_loopback.rs(pooled_client, against the realDhtHandlerwhere 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.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-shotcdn/probe/v1connection (the positive probe cache is keyed by hash), so the test subtracts the provider's inbounddecdn_probe_requests_totalfrom 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.node_origin_pull.rs: the fixture upstreams serve every stream a connection carries throughserve_each_stream, asClientHandlerdoes, 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_countersis removed, and its test counts streams from the handler's metrics.Testnet check after deploy (24 h against the 7-day baseline,
grafanacloud-prom)sum(increase(decdn_iroh_socket_num_conns_opened_total[1d]))sum(increase(decdn_iroh_socket_holepunch_attempts_total[1d])) / sum(increase(decdn_iroh_socket_num_conns_opened_total[1d]))histogram_quantile(0.5, sum by (le) (increase(decdn_node_pull_first_byte_seconds_bucket[1d])))(and 0.95)sum(rate(decdn_client_idle_close_total[1h]))on upstream nodesmax_over_time(decdn_dispatch_in_flight[1d])Then comment the before/after numbers on #2334 and #2336.
🤖 Generated with Claude Code