Repository navigation
refactor(node): decompose the ClientHandler mega-impl + construction-time ClientHandlerDeps (#1254) - #1323
Conversation
…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]>
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Free Run ID: 📒 Files selected for processing (15)
WalkthroughAdds 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. ChangesClient delivery protocol
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
Note 🎁 Summarized by CodeRabbit FreeYour 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 undercrates/node/src/handlers/client/. - Replaced post-construction
attach_*/OnceLockwiring with a single construction-timeClientHandlerDepsbundle (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)intoPaymentChannelService::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.
Coverage reportTotal: 81.47% (was 81.48%, ▼ 0.01%) — 49732/61040 lines
History
|
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) viagit mv(path-transparent), relocating 24 methods into six concern submodules — each a bareimpl ClientHandlerblock:dispatchserve,serve_streamwindowserve_via_window_pull_through,window_forward_loop,abandon_window_servefilldeliverydelivervouchercollect_voucher,handle_cooperative_closewirerecord_receipt,clamped_rate,sign_response,respond_error,write_reject,write_messageAll support types, consts, free functions, the struct + constructor, the channel-lifecycle methods, the
ProtocolHandlerimpl, and the test module stay inmod.rs. Moved methods becomepub(super); imports are explicituse super::{…}(the crate deniesclippy::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 10attach_*setters overOnceLockfields. This replaces that with a singleClientHandlerDeps(16 required + 12 optional) taken at construction:OnceLock<T>fields →Option<T>; the 10attach_*setters and test-onlyset_idle_timeoutare deleted; read sites switchOnceLock::get()→Option::as_ref().build_chain_and_handlerscomputes each optional asSome/Noneunder the exact same config gates and nesting as before, and passes one deps literal tonew.PaymentChannelService::bootstrapinto 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, andredeem_hint_sender()still returns the stored sender.bootstraptakes the(tx, rx)pair.configure: FnOnce(&mut ClientHandlerDeps)builder hook; aset_window_pull_throughhelper keepspull_through_origin/pull_ahead_bytesfrom being half-wired.Verification
cargo fmt --check,cargo clippy -p decdn-node --all-targets --features anvil-e2e -- -D warnings,cargo doc -D warnings— all cleane2e_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