Skip to content

feat(ipc): application-layer back-pressure groundwork (Phase 1+2 partial) - #670

Merged
zackees merged 1 commit into
mainfrom
feat/backpressure-protocol-2026-06-05
Jun 7, 2026
Merged

zackees merged 1 commit into
mainfrom
feat/backpressure-protocol-2026-06-05

Conversation

@zackees

@zackees zackees commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Draft — opening for design feedback, not ready to merge. Phases 3-4 (daemon emission, client retry loop) are TODO; this PR holds Phase 1 (pool sizing) + Phase 2 protocol surface only.

Problem

Under a parallel-build burst of ~670 TUs, the Windows named-pipe pool capped at .min(16) becomes the dominant bottleneck. Clients hit OS-level ERROR_PIPE_BUSY before the daemon ever sees the request, so "daemon overloaded" gets aliased with "daemon dead" at the client — every natural overload looks like a wedge and triggers the Layer A 90 s recv-timeout + force-kill recovery path from #669.

#669's recovery is the right safety net for actual wedges. This series prevents it from firing on healthy-but-overloaded daemons by moving the bottleneck inside the daemon where it can be expressed in-band.

Design

  • ACCEPT layer (this PR, Phase 1) — pool sized for expected burst (default cpu*4, clamp 16..=128, env override); goal: never reject at the OS layer
  • DISPATCH layer (next PR, Phase 3) — daemon reads request, evaluates real pressure metric (queue depth, lock waits), emits Response::Backpressure with retry_after_ms hint on overload
  • CLIENT layer (next PR, Phase 4) — honors retry_after_ms with jittered backoff and a budget; falls through to ephemeral compile on budget exhaustion

Properties this gives that the current design doesn't:

  • Failure mode separates from load mode. "Daemon dead" = ECONNREFUSED from accept; "daemon busy" = explicit BACKPRESSURE response. The two errors stop being aliased.
  • Adaptive client behavior. Retry budget moves from blind 50 retries / 12 s to honor what the daemon told the client.
  • Smart throttling. The daemon's pressure metric can be the actual narrow resource (queue depth, fingerprint-cache lock wait), not an OS-layer proxy.

This PR (Phase 1 + Phase 2 partial)

Phase 1 (transport.rs): .min(16) -> clamp(cpu*4, 16..=128) with ZCCACHE_PIPE_POOL_SIZE env override. cargo check clean.

Phase 2 (protocol/messages/mod.rs): New Response::Backpressure { queue_depth, retry_after_ms, reason } appended at end of the enum, following the existing convention (FingerprintCheckResult, RustArtifactList, GenericToolExecResult) so bincode variant indices stay stable for older clients.

TODO (separate commits)

  • Phase 3: Daemon dispatch reads queue depth, computes retry_after_ms = ((depth - threshold) * 50).min(5000) + jitter(50), emits Backpressure when depth > ZCCACHE_QUEUE_PRESSURE_THRESHOLD (default 256).
  • Phase 4: Client wrap path (cli/commands/wrap/ipc.rs) handles Backpressure: sleeps retry_after_ms, retries up to ZCCACHE_BACKPRESSURE_MAX_RETRIES (default 8), falls through to existing ephemeral path on budget exhaustion.
  • Tests: serialization, retry-then-success, budget-exhaustion-fallback, daemon-emits-on-threshold, regression: does-not-misclassify-wedge.
  • Version: 1.11.14 -> 1.12.0 once Phases 3-4 land.

Co-existence with #669

Backpressure and wedge-recovery don't collide: Backpressure is an in-band response (instant), wedge requires 90 s of silence. A healthy-but-overloaded daemon sends Backpressure within the wedge window so Layer A never fires; a truly hung daemon sends nothing so Layer A fires as designed.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

…partial)

Phase 1 — pipe pool sizing (transport.rs):
- The Windows named-pipe pool size cap was hard-coded `.min(16)`, which made
  the OS-level accept queue the dominant bottleneck under a 670-TU ninja
  burst. Clients hit `ERROR_PIPE_BUSY` before the daemon ever saw the
  request, which aliased "daemon overloaded" with "daemon dead" at the
  client (drove the Layer A/B/C wedge-recovery path from #669 on every
  natural overload, not just true wedges).
- Pool now defaults to `clamp(cpu_count * 4, 16..=128)` with env override
  via `ZCCACHE_PIPE_POOL_SIZE`. On a 16-core box that's 64 vs the previous
  16. Moves the bottleneck inside the daemon where overload can be
  expressed as a Response::Backpressure reply (Phase 2).

Phase 2 — protocol surface (protocol/messages/mod.rs):
- New `Response::Backpressure { queue_depth, retry_after_ms, reason }`
  variant, appended at end of the enum to preserve bincode variant
  indices for older clients (existing convention used by
  FingerprintCheckResult, RustArtifactList, GenericToolExecResult).
- Daemon-side emission (queue depth threshold check + jittered retry hint)
  and client-side retry loop with budget + fallback to ephemeral remain
  TODO — blocked on local disk space for cargo check during this session.

Design intent (full doc to land in a follow-up):

  ACCEPT layer (this commit)       — pool sized for expected burst,
                                      never reject at the OS layer
  DISPATCH layer (next commit)     — daemon reads request, evaluates
                                      real pressure metric, emits
                                      Backpressure on overload
  CLIENT layer (next commit)       — honors retry_after_ms with jittered
                                      backoff and a budget; falls through
                                      to ephemeral on budget exhaustion

Refs the daemon-wedge work in #669 (which catches true wedges via Layer
A's 90 s recv timeout + force-kill); this series catches the *high load*
case in-band so #669's recovery does not fire on natural overload.
@zackees
zackees marked this pull request as ready for review June 7, 2026 21:01
@zackees
zackees merged commit 0e1d273 into main Jun 7, 2026
26 checks passed
@zackees
zackees deleted the feat/backpressure-protocol-2026-06-05 branch June 7, 2026 21:01
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.

1 participant