feat(npm): hold the merge budget until the stream ends, part 5/5
Summary
Part 5 of 5 of a stack of merge requests (each targets the previous one; review and merge bottom-up) that bounds the npm virtual metadata merge's memory with a charged parse and a shared merge memory budget, as the Maven virtual merge already does (#1524 (closed)).
No plan file exists for this work: the work item graded as needing a spec amendment first, and the operator chose to ship the S13 (virtual-remote-foundation), S30 (maven-virtual) and S31 (npm-virtual) amendments in this stack instead of a plan MR.
This part makes a read hold its admitted reservation for the length of the merge. The read acquires the admitted prefix's total reservation, parks while the budget is held elsewhere, and releases only after the merge goroutine has finished, on every exit including a panic between acquire and start. A 304 never acquires. A parked read waits no later than its armed write deadline less a 5-second grace, then answers 503 upstream_unavailable with Retry-After, so it is shed while the answer can still reach the client. The shed logs its own Warn line, so it does not read as an upstream outage; the budget's metrics and alert are tracked in #1524 (comment 4030936233). A free budget admits at once through a new non-blocking virtual.MergeBudget.TryAdmit, so the bound limits only a park. Once admitted, the response write deadline is re-armed so the body gets a full window. Two functions are extracted from respond to keep it within the complexity limits.
Accepted behaviors, written into S13 and S31: a slot's hold time follows the client's pace within the body window, so a slow reader can delay other merges; the decoder's working set for the single input being parsed sits in the existing S13 headroom.
Governing ADRs
ADR 004 (entity-count limits, 20 upstreams per virtual): no conflict found; this part changes when memory is released, not what is admitted.
Testing
- Budget tests: a 304 never acquires, a slot is released on every exit after acquire, concurrent reads park and never omit, a canceled parked read releases nothing, a park past its bound answers
503withRetry-Afterand logs its own line, and the re-arm count.TryAdmithas its own tests, including that it never jumps a queued read. - An integration test proves one budget gates npm and Maven virtual reads together.
- Build, vet and the unit tests of the touched packages pass at this part;
go vet -tags integrationis clean at the tip. Integration-tagged tests were not run locally; CI runs them. - No e2e row is added or affected: budget exhaustion needs a controlled
GOMEMLIMITthat no black-box scenario can set. Nodocs/userchange: no user page covers npm virtual merge outcomes.
Plan and spec amendments
- S13 (virtual-remote-foundation): hold time follows the client's pace, a format may bound the park and npm ends one a grace before its armed write deadline with a
503, and the open item to re-measure the baseline headroom for npm. - S31 (npm-virtual): the hold-time sentences, the park bound with its acceptance criterion and error row, the budget is never held across the collection, and amended note 3.
Size
1320 reviewable lines (added plus removed against the merge base 3f79187a49, at 13c35c0212): 258 production, 923 tests, 80 specs, 59 other docs. The hold, release and re-arm paths share one lifecycle, and the tests that prove each exit releases exactly once are most of the diff.
Stacked MRs (review/merge bottom-up)
- refactor(virtual): share one merge memory budge... (!3184 - merged) • David Fernandez • 19.5
- feat(npm): charge the merge parse's retained st... (!3185 - merged) • David Fernandez • 19.5
- feat(npm): hold the merge tail at its encoded s... (!3186 - merged) • David Fernandez • 19.5
- feat(npm): admit the virtual merge against the ... (!3187 - merged) • David Fernandez, Dzmitry (Dima) Meshcharakou • 19.6
- feat(npm): hold the merge budget until the stre... (!3188 - merged) • David Fernandez, Dzmitry (Dima) Meshcharakou • 19.6
👈
Related to #1524 (closed)