Skip to content

feat(cli): decdn gateway runs pulls at once on one long-lived worker - #2462

Merged
alpergundogdu merged 1 commit into
mainfrom
claude/gateway-concurrent-pulls
Oct 14, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
claude/gateway-concurrent-pulls

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #2458.

decdn gateway ran 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.rs runs one thread with a current-thread runtime and a LocalSet (the fetch future is not Send). 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 one Connections, the PeerHealth table, the OrderedWrites queue, 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 a LaneLedgers registry, 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 old fetch_over. Each fetch keeps its own progress clock, so one that stalls gives up alone.

decdn get builds 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 Payer only 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 public decdn-client surface 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

  • Worker unit tests (gateway/worker/tests.rs, a stand-in backend):
    • a stalled response does not block another;
    • pulls past the bound wait for a slot;
    • the host is built once and closed on stop;
    • overlapping pulls under one capability share a payer, a later pull gets a new one, and another capability gets its own;
    • a host that cannot be built fails the start.
  • New e2e gateway_concurrent_pulls: a gateway subprocess pinned to one node.
    • The gateway mints a delegate, the owner issues a capability (pool assign), and the gateway registers it.
    • (1) Four concurrent requests under that capability (two 4 MiB blobs, twice each) all arrive byte-exact, and decdn_serve_stream_voucher_rejected_total does not move.
    • (2) A request finishes while an earlier response sits unread.
    • With per-pull ledgers (ledgers: None) the test fails: the node rejected 13 streams. With shared ledgers it passed 3 of 3 runs.
  • The existing blob e2e tests pass through the reworked fetch: cli_fetch_{stdout,resume,topup,delegated,multi_source,recovers_one_holder} and peer_store_probeless (11 tests).
  • cargo nextest run --workspace passes (4936 tests). clippy (--all-features), fmt and doc are clean.

🤖 Generated with Claude Code

…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]>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 13:43
@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main af677b6 claude/gateway-concu... 90f0634 +/-
75% 75% 0%
View coverage of the 8 most impacted files.
File main af677b6 claude/gateway-concu... 90f0634 +/-
crates/node/src...s/client/mod.rs 90% 89% -1%
crates/client/src/lib.rs 88% 88% 0%
crates/client/s...undle/engine.rs 61% 61% 0%
crates/protocol/src/framing.rs 93% 93% 0%
crates/cli/src/...ands/gateway.rs 43% 43% 0%
crates/cli/src/...mmands/fetch.rs 51% 51% 0%
crates/node/src/dht/publish.rs 78% 80% +2%
crates/cli/src/...teway/worker.rs 0% 80% +80%

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

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 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 one FetchHost per gateway run and shares Payer/LaneLedgers across overlapping pulls under one capability.
  • Refactor fetch.rs into FetchHost + Payer + fetch_on, replacing fetch_into/fetch_over; decdn get uses one host and a solo keystore payer.
  • Add worker unit tests and a live gateway_concurrent_pulls anvil e2e asserting no AmountRegression voucher 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.

@alpergundogdu
alpergundogdu merged commit 7b79f5a into main Oct 14, 2026
32 checks passed
@alpergundogdu
alpergundogdu deleted the claude/gateway-concurrent-pulls branch October 14, 2026 14:00
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.

gateway: concurrent pulls from one long-lived worker

2 participants