Skip to content

refactor(node): thread Bytes through leech-governor + window math (#910) - #923

Merged
thiras merged 2 commits into
mainfrom
feat/910-bytes-leech-governor
Jun 16, 2026
Merged

thiras merged 2 commits into
mainfrom
feat/910-bytes-leech-governor

Conversation

@thiras

@thiras thiras commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

What

Pushes the Bytes newtype (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 bare u64 the moment it crosses into the governor's internal state and the client.rs window-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} and PeerLeech.{pulled,served} become Bytes; the poll_admission budget/allowance math and the record_pulled/record_served accumulators follow. Public method params stay u64 (wrapped internally) so region_accounting.rs and the admin-RPC wire DTOs are untouched.
  • handlers/client.rs — the window-paced serve loop's pull_ahead_bytes (OnceLock<Bytes>), the window, and the pulled/served_paid frontier become Bytes. The voucher-interval locals (unvouchered/pending/delta) stay u64 — they belong to the voucher/payment domain, not the window-budget domain.
  • config-types/bytes.rs — adds typed Bytes::saturating_add/saturating_sub (both sides Bytes, 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:

  • Item 2 — Percent::scale_saturating now takes/returns Bytes instead of u64 (single production caller).
  • Item 3 — WindowExceedsBudget payload fields are now Bytes.

Out of scope

Verification

  • cargo clippy --workspace --all-targets — clean (incl. a too_many_lines regression fixed by collapsing the deposit-guard chain).
  • cargo nextest run -p decdn-config-types -p decdn-node -p decdn-common — 1071 tests pass, including new Bytes::saturating_* tests, the retyped scale_saturating tests, and the WindowExceedsBudget typed-payload boundary test.
  • cargo fmt --check and cargo doc (pre-commit) clean — the latter caught a now-redundant intra-doc link target exposed by the new use crate::Bytes;.

Closes #910. Resolves #914 items 2 & 3.

🤖 Generated with Claude Code

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]>
Copilot AI review requested due to automatic review settings June 16, 2026 21:55

@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 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.

Comment thread crates/node/src/handlers/client.rs Resolved
Comment thread crates/node/src/handlers/client.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

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 use Bytes throughout (keeping public method params as u64).
  • Retype the client.rs window-paced pull-through loop’s window/pulled/served frontier math to Bytes, keeping voucher/payment-domain locals as u64.
  • Extend decdn-config-types with Bytes::{saturating_add,saturating_sub} and change Percent::scale_saturating to take/return Bytes, 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.

@github-actions

github-actions Bot commented Jun 16, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 85.71% (was 85.70%, ▲ +0.01%) — 37231/43436 lines

File Coverage Lines Δ
crates/config-types/src/bytes.rs 100.00% 46/46 ● 0.00%
crates/config-types/src/percent.rs 93.33% 42/45 ▲ +2.16%
crates/node/src/handlers/client.rs 77.18% 849/1100 ▼ 0.12%
crates/node/src/leech_governor.rs 95.36% 185/194 ▲ +0.68%
crates/node/src/runtime/mod.rs 48.98% 888/1813 ● 0.00%
History
Commit Total Δ vs base
7e28bba 85.71% ▲ +0.01%
337f16c (HEAD) 85.71% ▲ +0.01%

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]>
@thiras
thiras merged commit 74f8921 into main Jun 16, 2026
20 checks passed
@thiras
thiras deleted the feat/910-bytes-leech-governor branch June 16, 2026 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants