Repository navigation
feat(cli): decdn gateway runs pulls at once on one long-lived worker - #2462
Conversation
…2458) The gateway ran one pull at a time and rebuilt the config, relays, buyer store, endpoint and Ctrl-C watcher on a new thread for every request. A response whose reader stopped held the one slot until the 120 s give-up. The blob fetch splits into a FetchHost (endpoint, store, connections, health, write queue, built once) and a Payer (credential slot, recovery gate, open lock, and for a held key shared lane ledgers). decdn get builds one of each for its fetch. The gateway's worker thread holds one host for the run and runs each request as a local task, up to 16 at once; overlapping pulls under one capability share a payer, so the node rejects none of their vouchers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Code coverage overviewLanguages: Rust Rust / code-coverage/cargo-llvm-cov
View coverage of the 8 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. |
There was a problem hiding this comment.
🔵 Needs a closer look
It reworks the economically-sensitive paid-pull path with new cross-pull concurrency and shared voucher ledgers where a subtle error could cause over-payment or voucher rejection, so final human review is warranted.
0 open findings
What changed in this PR
This PR reworks decdn gateway so that content requests are served concurrently from a single long-lived pull worker, instead of rebuilding the whole fetch bring-up (OS thread, runtime, config, endpoint, redb buyer store, Ctrl-C watcher) once per request and serving one pull at a time. It closes #2458.
The blob fetch in commands/fetch.rs is split into three pieces: FetchHost (everything a fetch shares, built once — chain, relays, buyer store, one endpoint/Connections, PeerHealth, OrderedWrites, and the pool token read once via a OnceCell), Payer (who pays and what that payer's concurrent fetches share — the credential slot, RecoveryGate, open lock, and for held keys a shared LaneLedgers so same-lane vouchers advance through one monotonic issuer), and fetch_on (the former fetch_over, with its own progress clock so a stalled fetch gives up alone). A new gateway/worker.rs runs these on one current-thread runtime + LocalSet, bounded by MAX_PULLS = 16, keeping a capability's Payer alive only while pulls under it are in flight.
Changes:
- Introduce a long-lived
PullWorker(one thread,LocalSet, up to 16 concurrent pulls) that builds oneFetchHostper gateway run and sharesPayer/LaneLedgersacross overlapping pulls under one capability. - Refactor
fetch.rsintoFetchHost+Payer+fetch_on, replacingfetch_into/fetch_over;decdn getuses one host and a solo keystore payer. - Add worker unit tests and a live
gateway_concurrent_pullsanvil e2e asserting noAmountRegressionvoucher rejections and per-pull pipe isolation.
| File | Description |
|---|---|
crates/cli/src/commands/fetch.rs |
Splits the blob fetch into FetchHost, Payer, and fetch_on; shares the endpoint/store/token and per-payer ledgers/recovery/open-lock. |
crates/cli/src/commands/gateway/worker.rs |
New pull worker: Backend trait, PullWorker, concurrent local-task runner, and payer sharing/pruning by signer. |
crates/cli/src/commands/gateway.rs |
Wires the worker in, adds MAX_PULLS, splits serve out, and routes start_pull through the worker; stops the worker before store removal. |
crates/cli/src/commands/gateway/worker/tests.rs |
Unit tests for stalling isolation, the concurrency bound, build-once/close, payer sharing, and start failure. |
crates/cli/src/commands/gateway/tests.rs |
Updates test_gateway to async with a NoPulls backend-backed PullWorker. |
crates/e2e/tests/gateway_concurrent_pulls.rs |
New anvil e2e: four concurrent pulls under one capability with no voucher rejection, plus a stalled-response isolation check. |
CHANGELOG.md |
Documents the gateway concurrent-pulls change (#2458). |
I reviewed the concurrency-sensitive parts in detail — the Rc strong-count payer-pruning heuristic in release/payer_for (correct under single-threaded cooperative scheduling), the shared LaneLedgers/RecoveryGate/open_lock sharing (matching the established bundle-engine pattern), watermark persistence ordering before a payer drops, the deployment-wide token OnceCell being safe across different signers, and shutdown/Rc::try_unwrap(host) ordering. I did not find concrete defects, and all removed symbols (fetch_into, fetch_over, the config_path field) have no dangling references.
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Closes #2458.
decdn gatewayran one pull at a time and rebuilt everything for each request: a new OS thread and runtime, the config, relays and discovery, the redb buyer store, a new iroh endpoint and a Ctrl-C watcher. A response whose reader stopped (a preloaded<video>, a background tab) held the one slot until the 120 s no-progress give-up, and every other request waited behind it.What changes
One long-lived worker.
gateway/worker.rsruns one thread with a current-thread runtime and aLocalSet(the fetch future is notSend). It builds the host once at startup and runs each request as a local task, up to 16 at once. Each pull streams into its own pipe, so a stalled response holds only its own pull. Ctrl-C stops the worker: in-flight pulls drop (their guards queue what they paid), the write queue settles, and the endpoint closes before the store dir is removed.The blob fetch splits in two (
commands/fetch.rs):FetchHost: what every fetch shares, built once. That is the resolved chain, the relays, the buyer store, one endpoint with oneConnections, thePeerHealthtable, theOrderedWritesqueue, and the pool's settlement token, read once.Payer: who pays, and what that payer's fetches share. That is the credential slot, the recovery gate and the open lock. For a held key it also includes aLaneLedgersregistry, so overlapping pulls under one capability advance each lane's cumulative voucher through one issuer, as a bundle pull's entries do.fetch_on(host, payer, blob, sink, timings)is the oldfetch_over. Each fetch keeps its own progress clock, so one that stalls gives up alone.decdn getbuilds one host and one solo payer for its one fetch: no shared ledgers, the same flags and the same output.The worker keeps a capability's
Payeronly while pulls under it are in flight. When its last pull ends (after persisting its lanes' watermarks), the payer drops, so the map stays bounded by in-flight pulls and needs no hooks into delegate pruning.Open question from the issue
The shared-ledger multi-pull stays CLI-internal on the client's hidden seams (
LaneSources,LaneLedgers,Connections), as agreed. A publicdecdn-clientsurface can follow.A note on "never double-spend a lane"
Without shared ledgers, concurrent pulls on one lane do not overpay. The node rejects the trailing cumulative (
AmountRegression) and the client reseeds from the node's watermark and carries on. What the shared ledger removes is those rejected streams and their retries. The e2e test asserts exactly that.Tests
gateway/worker/tests.rs, a stand-in backend):gateway_concurrent_pulls: a gateway subprocess pinned to one node.pool assign), and the gateway registers it.decdn_serve_stream_voucher_rejected_totaldoes not move.ledgers: None) the test fails: the node rejected 13 streams. With shared ledgers it passed 3 of 3 runs.cli_fetch_{stdout,resume,topup,delegated,multi_source,recovers_one_holder}andpeer_store_probeless(11 tests).cargo nextest run --workspacepasses (4936 tests). clippy (--all-features), fmt and doc are clean.🤖 Generated with Claude Code