feat(npm): add the virtual packument handler and its write deadlines, part 1/2 (S31 plan: 15/19)

📚 Stacked MRs

S31 plan Step 15 ships as 2 stacked MRs so each stays reviewable. Part 1 targets main, and part 2 targets part 1's branch. Review and merge them bottom-up.

This is part 1 of 2 of S31 npm virtual plan, Step 15: packument handler. Spec: S31 (npm-virtual).

🔍 What

Part 1 adds the handler and the serve-path changes behind it. No route reaches them yet: npmVirtualPackumentSlot still answers the interim 501, and part 2 wires it.

  • VirtualPackumentHandler (internal/format/npm/virtual_packument.go) validates the package name first, so an invalid name answers 422 package_name_invalid with no statement and no upstream request. It negotiates the variant from Accept once and carries the kind-encoded target to every position, a remote position's cold fetch included.
  • newVirtualDocumentServe owns the shared seam and knob checks, the response budget and the body floor, taking the VirtualDocumentHandlerDeps every virtual document handler shares, and virtualDocumentServe.serveRead is the one entry point: it arms the response write deadline, then reads the upstream list, then serves. A virtual route therefore cannot arm after the list read. An HTTP/2 stream's write timer runs from the stream's start, so a later arm cannot revive a stream it already reset.
  • The first arm is resolve_timeout + single_flight_wait_timeout + request_total_timeout + 5s. It goes through armReadResponseDeadline, the helper the remote routes already used, generalized to take the log message and the failure counter. A failed virtual arm counts on its own gitlab_artifact_registry_npm_virtual_response_deadline_arm_failures_total, and logs one Warn per request. The remote routes keep their text and counter.
  • The serve arms once more after the collection returns, to the larger of server.timeouts.write and the collected documents' size at the hosted route's floor rate, so a large merge is not cut on a window sized for a small one. A non-positive server.timeouts.write panics at construction, like the other knobs.
  • A committed failure this service caused (a deadline kill or a merge-side failure) books internal_server_error instead of a success. The kill is decided once per failure by virtualWriteDeadlineKill, so the log line and the abort's flush level always agree. The remote tarball route keeps its predicates and signatures: abortStreamedRelay delegates unchanged to the new abortStreamedRelayAt, and both routes take a failed flush's log level from one abortFlushLevel rule, which remoteTarballAbortFlushLevel now calls with the remote kill check.
  • A merge input is bounded by bytes, by parse failure, and by npm.max_versions_per_package plus two members per JSON object, never below the 16,384 members the publish envelope admits per object, because the merge's retained state grows per key, not per byte. The floor keeps a small npm.max_versions_per_package from omitting an ordinary document through its version or dependency objects, and a document of many tiny versions is omitted before its key state grows. The per-read total across 20 positions stays inside the 64MB x 20 bound S31 already accepts. #1524 (closed) carries the Maven merge's charged parse and memory budget into S31.
  • mergeOneSurvivor and mergeDistTagsInput close their input reader with defer. Before, a panic in the merge pass, which the serve recovers into a 500, left the reader open until garbage collection.
  • A HEAD refusal carries the Content-Length its GET would.

📝 Plan and spec amendments

  • Step 15 Files: adds internal/format/npm/virtual_document_serve.go (Modify) for the constructor, the entry point, the second deadline arm, the committed-failure outcome and the merge input bound, and virtual_packument_merge.go (Modify) for both reader closes. It also lists the files the review rounds touched: remote_packument.go (the shared arm helper), remote_tarball.go (the abort helpers the virtual serve shares), virtual_collect.go, metrics.go and metrics.md, the package docs, and docs/dev/observability.md.
  • S31 merge input bound: the "generated, not spliced" paragraph names the per-object bound of npm.max_versions_per_package plus two members, floored at the publish envelope's 16,384, beside the byte cap. The "A malformed input cannot fail the read" acceptance criterion and the Security Considerations bound on merge state name the member bound too. The operator chose to carry these corrections here without a separate sign-off from the S31 author.
  • S31 ### Metrics: adds gitlab_artifact_registry_npm_virtual_response_deadline_arm_failures_total.
  • Step 15 Acceptance: narrows "The metadata routes merge and the tarball route does not" to its packument half, because the tarball route still answers 501 here. It adds two assertions part 2 carries:
    • The packument half of "A hosted miss alongside an unhealthy remote is a 404 on the tarball route and a 503 on the metadata routes". The spec moved the metadata routes to 503 after Step 13 part 2 merged, so nothing asserted it.
    • The resolution budget's expiry as the client receives it.
  • Step 15 Status row: records this MR and !3109 (merged).
  • Step 16 Files: virtual_disttags.go builds its serve through newVirtualDocumentServe and answers through serveRead, which own the arm order and the body floor. No Step 16 MR is open.

🧪 Tests

Behaviour Test (internal/format/npm/)
Validation and reads #4 (closed): invalid name refused before resolution TestVirtualPackumentHandler_InvalidPackageNameIsRefusedBeforeResolution
Validation and reads #1, refusal framing TestVirtualPackumentHandler_HeadFramesARefusalAsItsGetDoes
Packument merge #21 (closed): the variant is chosen once and reaches the assembly TestVirtualPackumentHandler_NegotiatedAcceptReachesTheAssembly
First deadline arm, before the upstream list read, on the virtual counter TestVirtualPackumentHandler_ArmsTheResponseDeadlineBeforeTheUpstreamListIsRead, TestArmReadResponseDeadline, TestVirtualDocumentServe_AFailedFirstArmBooksTheVirtualCounterAndMessage
Second deadline arm and body floor TestVirtualDocumentServe_ArmsTheBodyFloorOnceTheCollectionReturns, ..._TheBodyFloorNeverShortensTheCallersDeadline, ..._ArmsTheInputsSizedWindowPastAShorterFloor, ..._AFailedBodyFloorArmLogsAndStillServes, ..._AFailedFirstArmLeavesTheBodyArmQuiet
Committed-failure outcome, one kill verdict TestVirtualDocumentServe_ACommittedFailureBooksAnOutcomeOnlyWhenTheServiceCutTheBody, TestVirtualDocumentServe_ASourceReadTimeoutBeforeTheDeadlineLogsAtErrorAndFlushesAtWarn, TestAbortFlushLevel
Merge input objects bounded by max_versions_per_package + 2, floored at the envelope cap TestNpmVirtualMergeLimits_BoundsAnInputObjectByMaxVersionsPerPackage, TestVirtualDocumentServe_BoundsEachInputByTheConfiguredMaxVersions
Wiring faults and construction TestVirtualPackumentHandler_WiringFaultsAnswer500, TestNewVirtualPackumentHandler_PanicsOnAMissingSeam, TestNewVirtualPackumentHandler_AcceptsAnEmptyPublicRegistryURL
Merge readers closed on a panic TestMergePackuments_ClosesAnInputWhosePassPanics, TestMergeDistTags_ClosesAnInputWhosePassPanics (both fail against the inline close)
Metric registration TestRegisterMetrics_ExposesAllOperationalVectors

The end-to-end acceptance suite lives in part 2, beside the slot that makes the route reachable.

docs/testing/ is unchanged: the merge journey is cataloged once the three npm virtual routes serve, at Step 19. docs/user/ is unchanged: this part changes no customer-visible surface.

📏 Size

1,558 reviewable LoC, past the 500 guideline:

Group LoC
Go source 375
Go tests 1,009
Package docs 99
Plan, spec and observability catalog 75

The plan records that Step 15 has no extractable seam. Part 1 is the largest slice that lands no reachable route. Three review rounds added about 520 lines, mostly tests: the shared entry point, the virtual arm counter, the single kill verdict, the shared flush-level rule, the merge input bound and its floor, and the dist-tags reader close.

Related to #896 (closed)

Edited by David Fernandez

Merge request reports

Loading
Loading