Skip to content

feat(node): meter refused serve_stream paid-delivery requests (#876) - #884

Merged
thiras merged 2 commits into
mainfrom
fix/876-serve-stream-rejected-metrics
Jun 12, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/876-serve-stream-rejected-metrics

Conversation

@thiras

@thiras thiras commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #876. Every paid-delivery reject branch in serve_stream (crates/node/src/handlers/client.rs) refused with a signed StreamResponse { ok: false } and a tracing::warn! but incremented no metric — so a refused stream was invisible to operator dashboards. An operator couldn't alert on "rate of refused streams" or distinguish a misconfigured client from an abuse campaign (e.g. a peer hammering with unknown-channel requests). This is the operator-side counterpart to the #848/#875 silent-failure work: #875 stopped serving free bytes on a bad channel, but the refusal itself still produced zero queryable signal.

What changed

Added six per-reason reject counters. The iroh_metrics backend has no runtime labels, so one Counter per reason matches the established dispatch_rejected_* / dht_rate_limit_rejected_* convention:

decdn_serve_stream_rejected_evicted_since_probe_total
decdn_serve_stream_rejected_cache_miss_total
decdn_serve_stream_rejected_internal_error_total
decdn_serve_stream_rejected_blob_too_large_total
decdn_serve_stream_rejected_unknown_channel_total
decdn_serve_stream_rejected_owner_mismatch_total

A private ServeRejectReason enum is threaded into the central respond_error helper, which increments the matching counter at a single site (exhaustive match, no wildcard — a new variant is compiler-forced to add a counter). The enum is the single source of truth: ServeRejectReason::wire_error() derives the signed StreamError, so the three branches that deliberately collapse to the wire NotFound (to avoid leaking channel existence) stay wire-indistinguishable while remaining separately countable server-side. Passing only the reason makes an inconsistent error/reason pairing unrepresentable at the call sites.

The metric is bumped before the network write, so a refusal is counted even if the client has already disconnected and the write fails — the counter measures the refusal decision, not its successful delivery.

Tests

  • Unit (metrics.rs): all six counters start at zero on a fresh registry and each method bumps exactly its own counter.
  • End-to-end wiring (client_loopback.rs): five tests assert the correct counter advances on a real refusal — evicted_since_probe, cache_miss, blob_too_large, unknown_channel, owner_mismatch. This deliberately covers all three wire-conflated NotFound reasons, where a mis-wired call site would otherwise pass every existing test (identical wire behavior). Added a spawn_handler_server_with_metrics helper; the old spawn_handler_server delegates to it, so no other call sites churned.

Out of scope / follow-ups

  • InternalError has unit coverage but no end-to-end wiring test — its three sites need a faulting-cache test double not in the loopback harness. Low risk given the compiler-enforced wire_error mapping and 5/6 wiring coverage.
  • The per-stream-cap reset path (client.rs:682) is a load-shed reset (not a signed ok:false response) with no counter — a different mechanism, pre-existing, outside this issue's signed-refusal scope. Candidate for its own issue.

Verification

cargo clippy -p decdn-node --all-targets   # clean
cargo fmt -- --check                        # clean
cargo nextest run -p decdn-node             # 594 passed

🤖 Generated with Claude Code

Every paid-delivery reject branch in `serve_stream` refused with a signed
`StreamResponse { ok: false }` and a `tracing::warn!` but incremented no
metric, so a refused stream was invisible to operator dashboards — there was
no way to alert on "rate of refused streams" or distinguish a misconfigured
client from an abuse campaign. This is the operator-side counterpart to the
#848/#875 silent-failure work.

Add six per-reason reject counters (the `iroh_metrics` backend has no runtime
labels, so one Counter per reason matches the existing `dispatch_rejected_*` /
`dht_rate_limit_rejected_*` convention):

  decdn_serve_stream_rejected_{evicted_since_probe,cache_miss,internal_error,
  blob_too_large,unknown_channel,owner_mismatch}_total

A private `ServeRejectReason` enum is threaded into the central `respond_error`
helper, which increments the matching counter at one site. The reason is the
single source of truth: `wire_error()` derives the signed `StreamError`, so the
three branches that deliberately share the wire `NotFound` (to avoid leaking
channel existence) stay wire-indistinguishable while remaining separately
countable server-side — and an inconsistent error/reason pairing is
unrepresentable at the call sites.

Tests: a metrics unit test asserts all six counters start at zero and each
method bumps exactly its own counter; five loopback tests assert the correct
counter advances on a real refusal (all reasons except InternalError, which
needs a faulting-cache double — deferred).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 12, 2026 21:54

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a server-side classification for serve_stream refusals using a new ServeRejectReason enum. This allows fine-grained tracking of rejection reasons (such as cache misses, unknown channels, or owner mismatches) via dedicated metrics, while still collapsing sensitive reasons into a generic NotFound error on the wire to prevent leaking channel existence. The changes also include corresponding updates to metrics tracking, unit tests, and integration tests to verify the counter increments. I have no feedback to provide as there are no review comments.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

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.

Pull request overview

Adds per-reason observability for serve_stream paid-delivery refusals so operator dashboards can distinguish refusal causes (including wire-conflated NotFound cases) and alert on refusal rates.

Changes:

  • Introduces six new serve_stream_rejected_* counters in node metrics and exposes increment helpers.
  • Threads a private ServeRejectReason through serve_stream refusal paths and increments the appropriate counter centrally in respond_error.
  • Extends loopback integration tests to assert the correct counter increments for several real refusal scenarios; adds a test helper that returns the server Arc<Metrics>.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
crates/node/src/handlers/client.rs Adds ServeRejectReason and centralizes per-reason refusal metric increments in respond_error.
crates/node/src/metrics.rs Defines/export six new refusal counters plus unit tests verifying zero-exposure and per-counter increments.
crates/node/tests/client_loopback.rs Adds metrics-aware server spawn helper and E2E assertions that specific refusal paths bump the correct counter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 86.14% (was 86.12%, ▲ +0.02%) — 35061/40702 lines

File Coverage Lines Δ
crates/node/src/handlers/client.rs 75.44% 562/745 ▲ +0.37%
crates/node/src/metrics.rs 84.59% 763/902 ▲ +0.77%
History
Commit Total Δ vs base
4b5662e 86.14% ▲ +0.02%
1a15679 (HEAD) 86.14% ▲ +0.02%

Address PR review nits: name the three wire-conflated NotFound reasons
explicitly instead of the ambiguous "reasons below", and scope the
"finer-grained than the wire" claim to the three NotFound cases (the other
three reasons map 1:1 to distinct wire errors). Comment-only; no behavior
change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 68d78a2 into main Jun 12, 2026
19 checks passed
@thiras
thiras deleted the fix/876-serve-stream-rejected-metrics branch June 12, 2026 22:15
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.

node: serve_stream paid-delivery rejections increment no metric — refused streams invisible to operators

2 participants