Skip to content

refactor(node): decompose the ClientHandler mega-impl + construction-time ClientHandlerDeps (#1254) - #1323

Merged
thiras merged 2 commits into
mainfrom
refactor/client-handler-split-1254
Jul 19, 2026
Merged

thiras merged 2 commits into
mainfrom
refactor/client-handler-split-1254

Conversation

@thiras

@thiras thiras commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1254.

Behavior-preserving refactor of crates/node/src/handlers/client.rs, delivered as two reviewable commits.

1 · Module split (9899526)

client.rs (3,687 lines) → client/mod.rs (1,652) via git mv (path-transparent), relocating 24 methods into six concern submodules — each a bare impl ClientHandler block:

submodule methods
dispatch serve, serve_stream
window serve_via_window_pull_through, window_forward_loop, abandon_window_serve
fill cache-fill tiers + background-warm machinery
delivery deliver
voucher collect_voucher, handle_cooperative_close
wire record_receipt, clamped_rate, sign_response, respond_error, write_reject, write_message

All support types, consts, free functions, the struct + constructor, the channel-lifecycle methods, the ProtocolHandler impl, and the test module stay in mod.rs. Moved methods become pub(super); imports are explicit use super::{…} (the crate denies clippy::wildcard_imports). Pure relocation — passed with zero test-file edits.

2 · Construction-time ClientHandlerDeps (e89b92e)

The handler was built with a 16-arg new() then wired via a chain of 10 attach_* setters over OnceLock fields. This replaces that with a single ClientHandlerDeps (16 required + 12 optional) taken at construction:

  • 12 OnceLock<T> fields → Option<T>; the 10 attach_* setters and test-only set_idle_timeout are deleted; read sites switch OnceLock::get() → Option::as_ref().
  • The runtime wiring in build_chain_and_handlers computes each optional as Some/None under the exact same config gates and nesting as before, and passes one deps literal to new.
  • The redeem-hint mpsc channel is hoisted out of PaymentChannelService::bootstrap into its callers — the sender is wired into the handler at construction (breaking the handler↔service cycle that forced the last post-construction attach), the receiver drives the redeemer loop, and redeem_hint_sender() still returns the stored sender. bootstrap takes the (tx, rx) pair.
  • Tests migrate to a configure: FnOnce(&mut ClientHandlerDeps) builder hook; a set_window_pull_through helper keeps pull_through_origin/pull_ahead_bytes from being half-wired.

Verification

  • cargo fmt --check, cargo clippy -p decdn-node --all-targets --features anvil-e2e -- -D warnings, cargo doc -D warnings — all clean
  • 865/865 node tests pass
  • 4/4 anvil settlement e2e tests pass, including the full on-chain e2e_onchain_payment_channel_settlement (the oracle for the redeem-hint hoist)

Review

Reviewed by five specialized agents (correctness, silent-failure, type-design, test-migration, comments) — consensus: behavior-preserving, no correctness/silent-failure/migration defects. Their findings (a type-design pairing helper and comment rot from the removed setters) are folded into commit 2.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added paid blob delivery over client streams, including verified range requests and chunked transfers.
    • Added voucher-based payment tracking with acknowledgements, receipts, and cooperative channel closing.
    • Added cache-miss recovery through local, peer, and origin sources, including progressive delivery while content is retrieved.
    • Added connection and stream safeguards, request validation, size limits, rate controls, and idle connection handling.
  • Bug Fixes
    • Improved handling of unavailable content, malformed requests, payment failures, and transient storage errors.

thiras and others added 2 commits July 19, 2026 18:26
…ules (#1254)

`crates/node/src/handlers/client.rs` concentrated paid delivery in one
~2,300-line `impl ClientHandler` block mixing several distinct concerns.
Rename `client.rs` -> `client/mod.rs` (path-transparent) and relocate the
method bodies into six concern-focused submodules, each a bare
`impl ClientHandler` block over the fields that stay in `mod.rs`:

  dispatch  serve, serve_stream
  window    serve_via_window_pull_through, window_forward_loop, abandon_window_serve
  fill      the cache-fill tiers + background-warm machinery
  delivery  deliver
  voucher   collect_voucher, handle_cooperative_close
  wire      record_receipt, clamped_rate, sign_response, respond_error, write_reject, write_message

All support types, consts, free functions, the struct + its constructor/
attach_* wiring, the channel-lifecycle methods, the ProtocolHandler impl,
and the test module stay in `mod.rs` so every submodule reaches them via
`use super::{...}`. Moved methods become `pub(super)` (sibling modules can't
see each other's private methods); imports are explicit because the crate
denies `clippy::wildcard_imports`. Intra-doc links in moved comments are
`super::`-qualified so they resolve in the submodule scope. `mod.rs` drops
3,687 -> 1,652 lines.

Behavior-preserving relocation only — no logic changes. All 865 node tests
pass with zero test-file edits; clippy `-D warnings`, fmt, and cargo doc
are clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ch_* setter chain (#1254)

The `ClientHandler` was built with a 16-arg `new()` and then wired via a
chain of 10 `attach_*` setters over `OnceLock` fields — its full
configuration was spread across a post-construction setter chain instead of
one reviewable literal.

Introduce `ClientHandlerDeps` (16 required + 12 optional fields): `new(deps)`
takes it, the 12 `OnceLock<T>` fields become `Option<T>`, and every optional
is supplied `Some`/`None` at construction. The 10 `attach_*` setters and the
test-only `set_idle_timeout` are deleted; read sites switch `OnceLock::get`
to `Option::as_ref`. `BackgroundFill` grows a `new()` constructor (the old
`attach_background_fill` body) and is `pub(crate)` so the runtime wiring can
build it.

The runtime wiring in `build_chain_and_handlers` now computes each optional as
`Some`/`None` under the exact same config gates (and nesting) as before and
passes one `ClientHandlerDeps` literal to `new`.

The redeem-hint mpsc channel is hoisted out of `PaymentChannelService::
bootstrap` into its callers: the sender is wired into the handler at
construction (breaking the handler↔service cycle that forced the last
post-construction attach), the receiver drives the redeemer loop, and
`redeem_hint_sender()` still returns the stored sender. `bootstrap` takes the
`(tx, rx)` pair.

Behavior-preserving. Tests migrate to a `configure: FnOnce(&mut
ClientHandlerDeps)` builder hook (`build_handler_full_configured`); the
node-B leech-governor helper takes `Option<LeechCaps>` and returns the
governor it builds over B's metrics. All 865 node tests and the 4 anvil
settlement e2e tests (incl. full on-chain settlement) pass; clippy
`-D warnings`, fmt, and cargo doc are clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings July 19, 2026 16:37
@coderabbitai

coderabbitai Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: f933944b-f519-4cf3-9926-bd4618aaa079

📥 Commits

Reviewing files that changed from the base of the PR and between 469e7d3 and e89b92e.

📒 Files selected for processing (15)
  • crates/node/src/handlers/client.rs
  • crates/node/src/handlers/client/delivery.rs
  • crates/node/src/handlers/client/dispatch.rs
  • crates/node/src/handlers/client/fill.rs
  • crates/node/src/handlers/client/mod.rs
  • crates/node/src/handlers/client/voucher.rs
  • crates/node/src/handlers/client/window.rs
  • crates/node/src/handlers/client/wire.rs
  • crates/node/src/payment_settlement.rs
  • crates/node/src/runtime/mod.rs
  • crates/node/tests/anvil_settlement_e2e.rs
  • crates/node/tests/client_loopback.rs
  • crates/node/tests/node_origin_pull.rs
  • crates/node/tests/origin_range_pull.rs
  • crates/node/tests/support/mod.rs

Walkthrough

Adds a complete paid client delivery handler with concurrent QUIC stream dispatch, tiered cache filling, signed responses, voucher settlement, range streaming, window pull-through, cooperative close handling, and construction-time runtime/test dependency wiring.

Changes

Client delivery protocol

Layer / File(s) Summary
Handler foundation and protocol framing
crates/node/src/handlers/client/mod.rs
Defines handler dependencies, lifecycle state, protocol framing, fill classifications, background-fill infrastructure, and unit tests.
Cache fill and admission paths
crates/node/src/handlers/client/fill.rs
Adds background warming, local and upstream population, range pull-through, timeout classification, fill coalescing, and leech admission hooks.
Stream dispatch and response delivery
crates/node/src/handlers/client/dispatch.rs, crates/node/src/handlers/client/delivery.rs, crates/node/src/handlers/client/wire.rs
Accepts concurrent streams, validates requests and channels, signs responses, exports verified ranges, sends chunks, and maps errors to protocol messages.
Voucher settlement and window pull-through
crates/node/src/handlers/client/voucher.rs, crates/node/src/handlers/client/window.rs
Persists cumulative vouchers, handles cooperative-close authorization, and forwards progressive upstream data through downstream delivery and cache teeing.
Construction-time dependency wiring
crates/node/src/runtime/mod.rs, crates/node/src/payment_settlement.rs, crates/node/tests/support/mod.rs
Builds ClientHandlerDeps with optional integrations and passes redemption channels explicitly into settlement bootstrap.
Integration and end-to-end configuration updates
crates/node/tests/client_loopback.rs, crates/node/tests/node_origin_pull.rs, crates/node/tests/origin_range_pull.rs, crates/node/tests/anvil_settlement_e2e.rs
Updates test builders and scenarios to configure handlers, pull-through, voucher activity, local population, prefetching, redemption hints, and leech governors during construction.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ClientHandler
  participant CacheEngine
  participant ChannelState
  Client->>ClientHandler: StreamRequest
  ClientHandler->>CacheEngine: inspect or fill blob
  ClientHandler->>ChannelState: resolve channel and authorization
  ClientHandler-->>Client: signed StreamResponse
  ClientHandler->>CacheEngine: export_bao_range
  ClientHandler-->>Client: ChunkData and StreamEnd
Loading

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

@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 refactors the ClientHandler by splitting its implementation into modular submodules and transitioning from post-construction attachment setters to a unified construction-time configuration via ClientHandlerDeps. The review feedback suggests caching the asynchronous pull_authorized check in serve_stream to prevent redundant lock acquisitions across cache-miss tiers, and reordering operations in forget_channel to delete from the persistent store before updating in-memory state for transactional consistency.

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.

Comment thread crates/node/src/handlers/client/mod.rs Resolved

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

Refactors the cdn/client/v1 node handler to improve reviewability and make handler wiring explicit at construction time (via ClientHandlerDeps), including hoisting the redeem-hint channel so the handler↔settlement-service cycle no longer requires post-construction mutation.

Changes:

  • Split ClientHandler’s large implementation into concern-focused submodules under crates/node/src/handlers/client/.
  • Replaced post-construction attach_*/OnceLock wiring with a single construction-time ClientHandlerDeps bundle (plus test builders that can “configure” optionals).
  • Hoisted the settlement redeem-hint channel creation to callers and updated runtime + e2e/tests to pass (redeem_tx, redeem_rx) into PaymentChannelService::bootstrap.

Reviewed changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/node/src/handlers/client/mod.rs New module root for the split handler; introduces ClientHandlerDeps and keeps shared types/consts/docs/tests.
crates/node/src/handlers/client/dispatch.rs Connection accept loop + per-stream dispatch; moved out of mod.rs.
crates/node/src/handlers/client/delivery.rs Delivery/export-range path extracted into its own impl block.
crates/node/src/handlers/client/fill.rs Cache-fill tiers + background warm machinery extracted into its own impl block.
crates/node/src/handlers/client/window.rs Window-paced pull-through serve path extracted into its own impl block.
crates/node/src/handlers/client/voucher.rs Voucher collection/payment loop + cooperative-close handling extracted into its own impl block.
crates/node/src/handlers/client/wire.rs Wire helpers (rate clamp, signing, message writes, receipts) extracted into its own impl block.
crates/node/src/runtime/mod.rs Builds ClientHandlerDeps in one literal; creates redeem-hint channel and passes tx/rx to settlement bootstrap.
crates/node/src/payment_settlement.rs PaymentChannelService::bootstrap now takes redeem-hint tx/rx from the caller instead of minting them internally.
crates/node/tests/support/mod.rs Adds shared client_handler_deps and build_handler_full_configured to support construction-time optional wiring in tests.
crates/node/tests/origin_range_pull.rs Migrates to build_handler_full_configured to set pull-through deadline at construction.
crates/node/tests/node_origin_pull.rs Migrates attach-based wiring to construction-time deps; threads optional leech governor through builders where needed.
crates/node/tests/client_loopback.rs Adds configured handler builders; migrates idle-timeout/voucher-activity/region-accountant wiring to deps.
crates/node/tests/anvil_settlement_e2e.rs Creates redeem-hint channel at the test level and passes tx/rx into handler deps + settlement bootstrap.

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

@github-actions

Copy link
Copy Markdown

Coverage report

Total: 81.47% (was 81.48%, ▼ 0.01%) — 49732/61040 lines

File Coverage Lines Δ
crates/node/src/handlers/client.rs — — deleted
crates/node/src/handlers/client/delivery.rs 95.38% 62/65 new
crates/node/src/handlers/client/dispatch.rs 84.84% 207/244 new
crates/node/src/handlers/client/fill.rs 59.89% 106/177 new
crates/node/src/handlers/client/mod.rs 87.83% 570/649 new
crates/node/src/handlers/client/voucher.rs 80.12% 133/166 new
crates/node/src/handlers/client/window.rs 84.56% 219/259 new
crates/node/src/handlers/client/wire.rs 94.12% 112/119 new
crates/node/src/payment_settlement.rs 56.98% 943/1655 ▼ 0.03%
crates/node/src/runtime/mod.rs 45.81% 1081/2360 ▼ 0.12%
History
Commit Total Δ vs base
e89b92e (HEAD) 81.47% ▼ 0.01%

@thiras
thiras merged commit 11caeb7 into main Jul 19, 2026
24 checks passed
@thiras
thiras deleted the refactor/client-handler-split-1254 branch July 19, 2026 16:48
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.

refactor(node): split the 2,300-line ClientHandler impl into delivery/voucher/window submodules + a deps struct

2 participants