Repository navigation
feat(payments): bound per-lane credit exposure with a concurrent-stream admission cap - #1700
Conversation
…o miss paths are capped The gate previously ran only right before the hit-path success response, so serve_via_backend_origin and serve_via_window_pull_through returned before reaching it and bypassed the aggregate cap entirely. Move the check-and- increment to run once, immediately after known_lane resolves and before any serve-path branch, so every delivered stream is counted regardless of path. Add a regression test covering the previously-uncovered miss path: two concurrent same-lane requests for a locally-missing hash on a one-floor budget admit exactly one and refuse the surplus with NotFound.
There was a problem hiding this comment.
Pull request overview
This PR hardens the node-side payments/serving path by enforcing a per-lane (pool_id, signer, provider) concurrent-stream admission cap, preventing multiple same-lane streams from collectively exceeding the refundable-floor headroom while keeping wire behavior unchanged (refusals still collapse to NotFound).
Changes:
- Add a per-lane
active_streamscounter with an RAIILaneSlotguard to ensure slots are released on all exit paths. - Hoist a single admission gate in the client dispatch path so it covers all serve branches (hits and misses), rejecting surplus opens as
LaneAtCapacity→ wireNotFound. - Add/refine tests covering refusal behavior, determinism under concurrent opens, and slot release semantics; add a dedicated metric for lane-capacity rejections.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| crates/node/src/handlers/client/mod.rs | Adds active_streams to lane state, introduces LaneSlot, and adds focused unit tests for wire-collapse + drop behavior. |
| crates/node/src/handlers/client/dispatch.rs | Implements the hoisted per-lane admission cap under the lane lock and wires LaneSlot lifetime into stream handling. |
| crates/node/src/handlers/client/wire.rs | Records the new LaneAtCapacity reject reason via a dedicated metric. |
| crates/node/src/metrics.rs | Adds serve_stream_rejected_lane_at_capacity counter and registers it for export/testing. |
| crates/node/src/handlers/client/voucher.rs | Updates test lane fixtures to initialize the new active_streams field. |
| crates/node/tests/client_loopback.rs | Adds helpers and multiple tests for same-lane concurrency admission/refusal and slot release behavior. |
| crates/node/tests/node_origin_pull.rs | Adds an integration-style test ensuring the cap also applies to window pull-through miss paths. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| impl Drop for LaneSlot { | ||
| fn drop(&mut self) { | ||
| self.counter.fetch_sub(1, Ordering::Relaxed); | ||
| } | ||
| } |
There was a problem hiding this comment.
Good catch — addressed in 49e7dd0. LaneSlot::drop now uses a saturating fetch_update that floors the counter at 0, so an unpaired slot from a future refactor fails safe instead of wrapping to u32::MAX and wedging the lane. Behavior is unchanged in the always-paired case that holds today.
…-stream-credit-bd18c1 # Conflicts: # crates/node/tests/client_loopback.rs
A LaneSlot only ever exists paired with a prior fetch_add, so the active-stream counter is never 0 at drop today. Use a saturating fetch_update so a future refactor that constructs an unpaired slot fails safe (counter floors at 0) rather than underflowing to u32::MAX and wedging the lane into refusing every admission. Raised in PR review. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Coverage reportTotal: 80.62% (was 80.60%, ▲ +0.02%) — 58735/72857 lines
History
|
Closes #1697.
Problem
The downstream credit window was enforced per stream, not per lane. The open-time solvency gate reserved one credit-window floor per request, and nothing aggregated concurrent same-lane draws. So N concurrent streams on one lane
(pool_id, signer, provider)could put up to N × floor of unpaid egress in flight while the refundable-minimum floorMonly ever guarded a single floor.Latent today (the only shipped multi-stream client, CLI bundle pull, serializes same-lane access with
provider_lock), but reachable by any non-serializing or hostile client.Approach — an admission cap, not a divided window
A stream's credit window is fixed at admission and never re-divided among live streams. The lane mutex guards an atomic check-and-increment of the active-stream count against
remaining − M— not a recomputation of everyone's share. Two admitted streams each keep their own full window; a third arrival is admitted with its own full window if budget covers one more floor, or refused — never "all three re-share a smaller slice." The serve loop is untouched.Arc<AtomicU32>) behind the existing per-lane mutex.(n_active + 1) × floor; the surcharge applies only when another same-lane stream is already active, son = 1is byte-for-byte the prior behavior.LaneSlotdrop guard releases the slot lock-free on every exit path (success,?-return, disconnect, panic) — a leaked slot would make the lane refuse forever.ServeRejectReason::LaneAtCapacitycollapses to wireNotFound(likeInsufficientDeposit): no wire-format change, preserves balance/concurrency-state privacy, and the client already treatsNotFoundas transient/retryable ("back off / go serial").The cap is dynamic:
max_concurrent = floor((remaining − M) / floor_payment). As the pool drains the ceiling drops until even one stream is refused — the existing single-stream deposit refusal falling out of the same formula.Out of scope (unchanged)
Tests
second_same_lane_stream_refused_when_budget_covers_one— surplus refused (NotFound) while the admitted stream settles.finished_stream_releases_its_lane_slot— a later open succeeds after the first settles (provesDropreleases the slot).single_same_lane_stream_admitted_unchanged—n = 1identity.concurrent_opens_admit_exactly_one— two simultaneous opens, exactly one admitted (deterministic under the lane lock).concurrent_same_lane_misses_refuse_surplus— the same cap on the node→node miss path (origin/window pull-through).Full
decdn-nodesuite green (one unrelated pre-existing timing flake,a_slow_post_topup_delivery_scores_slow_not_instant).🤖 Generated with Claude Code