Repository navigation
feat(gateway): cache what pulls deliver and serve repeats from it - #2905
alpergundogdu wants to merge 2 commits into
Conversation
decdn gateway keeps a decdn-cache CacheEngine under <data-dir>/cache (--cache-mb, default 10240; 0 turns it off) with the node's eviction loop on it. Every pull streams into it through CacheStores, a StreamStore over admit_bao_stream_until. A request whose bytes the cache holds, whole or its one Range, is served from it with no pull and no allowance charge, and counts in decdn_gateway_cache_hits_total. The CLI may now link iroh-blobs (decdn-cache without its s3 feature); the client SDK and sponsord still may not. contiguous_byte_ranges ends an open span at the blob's size, which the cache's bitfield needs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Note
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
🟡 Changes recommended
Approval is blocked by unresolved critical lifecycle/concurrency risk where the cache eviction task may continue mutating state during engine shutdown due to not being awaited, alongside an overly restrictive Sync bound on streamed responses that can break valid non-Sync stream implementations.
2 open issues
What changed in this PR
Adds a persistent, size-bounded cache to decdn gateway so streamed pulls populate decdn-cache under <data-dir>/cache and repeat requests (including single-Range) can be served directly from cache without starting a pull or charging allowance/capability, alongside a client range-span bug fix plus metrics/docs/tests and dependency-edge updates.
Changes:
- Add gateway cache configuration (
--cache-mb,0disables), eviction integration, and shutdown flushing. - Route streamed pulls to fill the cache and serve cache hits (whole blob or single
Range) with identical headers and a new cache-hit metric + dashboard tile. - Fix open-ended range span handling in
decdn-clientand update tests, docs, and crate-edge guards for the CLI→cache linkage.
| File | Description |
|---|---|
| docker/README.decdn.md | Documents gateway cache persistence/location and how repeat requests are served from cache in Docker deployments. |
| crates/node/Cargo.toml | Updates node features to enable decdn-cache's s3 support where required. |
| crates/e2e/tests/gateway_public.rs | Adds an anvil-backed public gateway e2e journey asserting cache hits avoid pulls and do not change allowance or node bytes. |
| crates/common/src/cli/gateway/tests.rs | Adds CLI parsing tests covering --cache-mb defaults and disabling the cache with 0. |
| crates/common/src/cli/gateway.rs | Introduces the --cache-mb gateway flag and documents cache semantics in help text. |
| crates/client/src/streamer.rs | Clarifies the StreamStores contract, including how shared stores behave under concurrent fills. |
| crates/client/src/driver/tests.rs | Adds a regression test ensuring an open-ended span runs to the end of the blob. |
| crates/client/src/driver.rs | Fixes contiguous_byte_ranges so an open-ended span is treated as extending to total_bytes. |
| crates/cli/src/commands/gateway/worker.rs | Threads an optional cache-backed StreamStores into gateway host/worker construction so pulls can fill the cache. |
| crates/cli/src/commands/gateway/tests.rs | Adds unit tests validating cached whole-blob and range serving, plus partial/miss behavior falling back to pulls. |
| crates/cli/src/commands/gateway/status/tests.rs | Updates status snapshot tests to include the new cache-hits counter field. |
| crates/cli/src/commands/gateway/metrics/tests.rs | Extends metrics exposition tests to assert decdn_gateway_cache_hits_total behavior and naming. |
| crates/cli/src/commands/gateway/metrics.rs | Adds a cache-hit counter and exposes it in both status JSON and Prometheus/OpenMetrics output. |
| crates/cli/src/commands/gateway.rs | Implements gateway cache open/eviction/shutdown and the cache-hit serving path (including HEAD/Range handling). |
| crates/cli/src/commands/fetch/cache_store/tests.rs | Adds tests for cache-backed streaming ingest, stop-at behavior, and reading back filled content. |
| crates/cli/src/commands/fetch/cache_store.rs | Implements CacheStores/CacheStore as a StreamStore over CacheEngine for streaming fills and reads. |
| crates/cli/src/commands/fetch.rs | Plumbs an optional stream cache into the fetch path so gateway streamed fetches populate cache instead of only scratch storage. |
| crates/cli/README.md | Updates CLI documentation to reflect linking store-only decdn-cache (AWS-free) and revised blob-store linkage statements. |
| crates/cli/dashboard/src/view.test.ts | Updates dashboard view tests/fixture expectations to include the cache-hits counter. |
| crates/cli/dashboard/src/fixtures/status-public.json | Adds cache_hits to the public status fixture counters. |
| crates/cli/dashboard/src/fixtures/status-pool-error.json | Adds cache_hits to the pool-error status fixture counters. |
| crates/cli/dashboard/src/fixtures/status-loopback.json | Adds cache_hits to the loopback status fixture counters. |
| crates/cli/dashboard/src/contract.test.ts | Extends dashboard contract tests to require/support the new cache_hits counter field. |
| crates/cli/dashboard/src/components/StatTiles.tsx | Adds a “Cache hits” stat tile and wires it to the status counters model. |
| crates/cli/dashboard/src/components/StatTiles.test.ts | Updated as part of this pull request. |
| crates/cli/dashboard/src/api.ts | Extends dashboard API types/models to include the cache_hits counter. |
| crates/cli/Cargo.toml | Adds decdn-cache (store-only) and related dependencies for the CLI and adjusts dependency/feature commentary accordingly. |
| crates/bao-range/src/lib.rs | Updates crate-level documentation to align with revised blob-store linkage constraints across components. |
| CLAUDE.md | Updates dependency-flow documentation to reflect the now-allowed CLI→decdn-cache edge and related constraints. |
| Cargo.toml | Adjusts workspace dependency configuration so decdn-cache is pulled in store-only by default (default-features = false). |
| Cargo.lock | Updates the lockfile to reflect new/adjusted dependencies introduced by the gateway cache work. |
| adr/appendix-gateway.md | Documents gateway cache behavior, hit rules, configuration, and associated observability fields. |
| adr/appendix-binaries.md | Updates the binaries appendix to reflect the CLI’s updated dependency/linkage footprint. |
| adr/038-bao-verified-range-streaming.md | Adjusts ADR wording to match the updated component boundaries and dependency assumptions after cache integration. |
| .github/scripts/tests/test_check_crate_edges.py | Updates crate-edge tests to allow the CLI’s new linkage while continuing to guard against forbidden AWS dependencies. |
| .github/scripts/check_crate_edges.py | Extends the dependency-edge checker to permit the CLI’s allowed blob-store/cache linkage and keep other roots constrained. |
Review effort: Balanced
| // The worker's queued lane writes land before the store dir is removed. | ||
| gateway.pulls.stop().await; | ||
| drop(temp_store); | ||
| drop(eviction); | ||
| // The cache's last writes reach its database before the process exits. | ||
| if let Some(engine) = &cache | ||
| && let Err(e) = engine.shutdown().await | ||
| { | ||
| tracing::warn!(error = %e, "the gateway cache did not shut down cleanly"); | ||
| } |
There was a problem hiding this comment.
Fixed: start_eviction now returns the task handle too, and close_cache sends the stop and awaits the loop before calling shutdown(), so no release races the last writes.
🤖 Addressed by Claude Code
| body: S, | ||
| ) -> Result<Response<Body>, PullError> | ||
| where | ||
| S: futures_util::Stream<Item = Result<Frame<Bytes>, std::io::Error>> + Send + Sync + 'static, | ||
| { |
There was a problem hiding this comment.
No change: the gateway's Body is http_body_util::combinators::BoxBody, and .boxed() requires Send + Sync, so streamed_response needs the Sync bound. That is also why a cache hit reads through a spawned task into a channel rather than polling the cache read future in the body.
🤖 Addressed by Claude Code
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 |
The pointer journey runs its gateway with --cache-mb 0, since it counts a repeat request against the namespace allowance. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Combined into #2908, which carries this PR's commits together with the rest of the stack so they land as one. |


The last step of #2881's plan for the gateway:
decdn gatewaykeeps what it pulls in adecdn-cacheCacheEngineand serves a repeat request from it. It builds on #2884 (the cache without its AWS SDK), #2886 (the eviction loop in the cache), #2887 (aStreamerthat fills a store the caller opens) and #2903 (a streamed admit that reports progress and stops early).What changes
<data-dir>/cache.--cache-mb(default 10240) sets its size, and--cache-mb 0turns it off. The node's eviction loop runs on it, with the node's default marks and free-disk margin. The gateway shuts the cache down on exit, so its last writes reach the database.fetch::CacheStoresandCacheStoreare aStreamStoreoverCacheEngine: each leg is admitted throughadmit_bao_stream_until, which verifies every chunk group, reports progress and honors a lowered end.FetchHost::with_stream_cachemakes every streamed fetch on the gateway's host fill it.decdn getkeeps its scratch directory.Range. A hit is served out of the cache with the same headers a pull gets. It starts no pull, spends no capability and charges no allowance. A blob held whole also answersHEADwith its length, with no size lookup.decdn-client:contiguous_byte_rangesnow ends a span with no end at the blob's size. Before, it dropped that span. The cache's bitfield gives the span that holds a blob's last chunk an open end, so a stream reading the cache stopped one span short (stream fetch finished with 1540096 of 1572864 bytes readable).ClientRangedStorenever yields an open span, sodecdn getwas not affected.decdn_gateway_cache_hits_totalcounter, in/_statusand on the dashboard.decdn-cli → decdn-cacheis now an allowed edge. The CLI may linkiroh-blobs; the client SDK and sponsord still may not, and nothing may link the AWS SDK. The workspace takesdecdn-cachewithout default features, and the node turns ons3. The table, its tests, theCLAUDE.mddependency paragraph and the docs that said the CLI links no blob store are all updated.StreamStorescontract: an implementation now documents whether two streams may fill one store at once. Concurrent pulls of one blob share one cache entry, and each admit is verified and idempotent.adr/appendix-gateway.md § Cache, the gateway module doc and the Docker README.Tests
cache_store: a drained leg lands in the cache for the next stream; a lowered end stops a leg at a chunk-group boundary; aStreamerover the cache reads back the whole 1.5 MB blob it fills (this test failed before thecontiguous_byte_rangesfix).driver: an open span runs to the end of the blob.gateway: a cached blob is served with no pull and no allowance; a cachedRangeis served as a 206; a partial entry that misses the request is no hit.gateway_publice2e, which needs anvil:--cache-mb 0, so its pull and allowance accounting is unchanged;GETand aRangecome from the cache, that the node writes no byte for them, and that the allowance stays put.cargo nextest run -p decdn-cli -p decdn-cache -p decdn-node, theanvil-e2egateway suite,cargo clippy --workspace --all-targets --all-featureswith warnings denied,cargo doc,check_crate_edges.pyand its pytest suite,cargo fmt --check, the dashboard tests.Not in this PR
🤖 Generated with Claude Code