Repository navigation
refactor(node): thread Bytes through leech-governor + window math (#910) - #923
Conversation
Pushes the `Bytes` newtype (#894/#911) past the config boundary into the hot-path byte arithmetic so the unrecouped-leech accounting is type-checked end-to-end, not just at the edge: - leech_governor: `LeechState.{global_pulled,global_served}` and `PeerLeech.{pulled,served}` become `Bytes`; `poll_admission` budget/allowance math and the `record_pulled`/`record_served` accumulators follow. Public method params stay `u64` (wrapped internally) so region accounting and the admin-RPC wire DTOs are untouched. - client.rs window-paced serve loop: `pull_ahead_bytes`, the `window`, and the `pulled`/`served_paid` frontier become `Bytes`; the voucher-interval locals (`unvouchered`/`pending`/`delta`) stay `u64` (voucher domain). - config-types: add `Bytes::saturating_add`/`saturating_sub` (typed both sides). Also folds in #914 items 2 & 3 (a clean #910 requires exactly these): - `Percent::scale_saturating` now takes/returns `Bytes` instead of `u64`. - `WindowExceedsBudget` payload fields are `Bytes`. Closes #910. Resolves #914 items 2 & 3. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors the codebase to use the strongly-typed Bytes wrapper instead of raw u64 values for byte quantities, specifically in the client handler, leech governor, and percentage scaling logic. This includes adding saturating_add and saturating_sub helper methods to Bytes. The review feedback suggests leveraging the Display implementation of Bytes in logging statements to avoid explicit .get() calls, making the code more idiomatic and concise.
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
This PR pushes the Bytes newtype deeper into the node’s seed-leech / window-paced pull-through hot path so internal byte arithmetic is unit-checked end-to-end (governor accumulators + client window math), reducing the risk of bytes↔percent or pulled↔served transpositions silently compiling and saturating.
Changes:
- Retype leech governor state (
global_*, per-peer counters) and its allowance/budget math to useBytesthroughout (keeping public method params asu64). - Retype the
client.rswindow-paced pull-through loop’s window/pulled/served frontier math toBytes, keeping voucher/payment-domain locals asu64. - Extend
decdn-config-typeswithBytes::{saturating_add,saturating_sub}and changePercent::scale_saturatingto take/returnBytes, plus update typed error payloads and affected call sites/tests.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/node/tests/node_origin_pull.rs | Updates the window pull-through test wiring to pass a typed Bytes window value. |
| crates/node/src/runtime/mod.rs | Passes cfg.cache.pull_ahead_bytes as Bytes into the client handler window pull-through attachment. |
| crates/node/src/leech_governor.rs | Retypes governor accumulators and per-peer accounting to Bytes, updates admission + record math, and types the WindowExceedsBudget payload. |
| crates/node/src/handlers/client.rs | Retypes window-budget arithmetic and state in the fused window-paced pull-through loop to Bytes. |
| crates/config-types/src/percent.rs | Changes Percent::scale_saturating to accept/return Bytes and updates tests accordingly. |
| crates/config-types/src/bytes.rs | Adds typed Bytes::saturating_add/saturating_sub helpers with tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Coverage reportTotal: 85.71% (was 85.70%, ▲ +0.01%) — 37231/43436 lines
History
|
Addresses the multi-agent review of #923 (no critical/important findings; these are the suggested polish items): - Add `Bytes::is_zero()` (const) and use it for the "0 disables the cap" sentinel checks in `LeechCaps::new` and `poll_admission`, keeping the budget logic in the type's vocabulary instead of `.get() > 0`. The adjacent `initial > max` compare stays `.get()`-based — derived `Ord` is not const-callable and `LeechCaps::new` is a `const fn`. - Document why `record_pulled`/`record_served` take `u64` (network-boundary `chunk.len()` cast, wrapped to `Bytes` internally) so the asymmetry vs the `Bytes`-typed `attach_window_pull_through` reads as intentional. - Annotate the deliberately-`u64` voucher-domain locals (`unvouchered`, `pending`) in the window loop, mirroring the `total` Bytes annotation. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
What
Pushes the
Bytesnewtype (introduced at the config boundary in #894/#911) deeper into the hot-path byte arithmetic, so the unrecouped-leech accounting is type-checked end-to-end rather than unwrapping to bareu64the moment it crosses into the governor's internal state and theclient.rswindow-paced serve loop.A bytes↔count or pulled↔served transposition in that arithmetic previously compiled, ran, and was silently absorbed by saturating math. With the accumulators typed, such a swap is now a compile error.
Changes
leech_governor.rs—LeechState.{global_pulled,global_served}andPeerLeech.{pulled,served}becomeBytes; thepoll_admissionbudget/allowance math and therecord_pulled/record_servedaccumulators follow. Public method params stayu64(wrapped internally) soregion_accounting.rsand the admin-RPC wire DTOs are untouched.handlers/client.rs— the window-paced serve loop'spull_ahead_bytes(OnceLock<Bytes>), thewindow, and thepulled/served_paidfrontier becomeBytes. The voucher-interval locals (unvouchered/pending/delta) stayu64— they belong to the voucher/payment domain, not the window-budget domain.config-types/bytes.rs— adds typedBytes::saturating_add/saturating_sub(both sidesBytes, so a unit confusion can't slip in).Folds in #914 items 2 & 3
A clean #910 requires exactly these, so they're done here rather than left to churn later:
Percent::scale_saturatingnow takes/returnsBytesinstead ofu64(single production caller).WindowExceedsBudgetpayload fields are nowBytes.Out of scope
region_accounting.rsand the admin-RPCRegionBytesDTOs (wireu64).deliver()non-window voucher accounting.Verification
cargo clippy --workspace --all-targets— clean (incl. atoo_many_linesregression fixed by collapsing the deposit-guard chain).cargo nextest run -p decdn-config-types -p decdn-node -p decdn-common— 1071 tests pass, including newBytes::saturating_*tests, the retypedscale_saturatingtests, and theWindowExceedsBudgettyped-payload boundary test.cargo fmt --checkandcargo doc(pre-commit) clean — the latter caught a now-redundant intra-doc link target exposed by the newuse crate::Bytes;.Closes #910. Resolves #914 items 2 & 3.
🤖 Generated with Claude Code