Skip to content

Cache rewritten npm and Composer metadata (fork CI check) - #17

Closed
montehurd wants to merge 1 commit into
mainfrom
cache-rewritten-metadata
Closed

montehurd wants to merge 1 commit into
mainfrom
cache-rewritten-metadata

Conversation

@montehurd

Copy link
Copy Markdown
Owner

Draft PR inside the fork, only to run CI on this branch before proposing it upstream. Fork main is behind upstream, so the commit list is long; CI tests the branch merged onto it. Will be closed once CI finishes.

The npm and Composer handlers rewrite every metadata document they serve
so that download URLs point at the proxy: decode the whole document into
generic maps, change the URLs, encode it again, and for Composer expand
the minified format first. That ran on every request, cached metadata
included. With metadata caching on and upstream out of the picture, a
cached request still cost 7 ms for @babel/core, 17 ms for
symfony/console and 155 ms and 134 MB of allocations for typescript, and
an 8 vCPU VM serving cached npm packuments to 20 clients ran the proxy
at 450% CPU for about 1,100 requests a second.

Rewritten documents are now kept in memory, keyed by the ecosystem,
proxy URL, package and a SHA-256 of the raw document, so new bytes from
upstream are rewritten again and nothing is served stale. Requests that
arrive while a document is being rewritten wait for that rewrite rather
than running their own; a waiter leaves when its client does, and the
rewrite still completes and is cached. The denylist is fixed at startup,
so it needs no place in the key. Cooldown filtering depends on the
current time, so with cooldown on the cache is bypassed.

metadata_rewrite_cache_size bounds the cache (default "256MB", least
recently used out first, "0" to rewrite on every request). NewProxy
callers keep the old behaviour unless they set it.

Whole cached requests, measured locally:
  @babel/core      7.1 ms -> 0.38 ms, 54,489 -> 119 allocations
  typescript       155 ms -> 10.5 ms, 1.37M -> 133 allocations
  symfony/console  17.4 ms -> 0.48 ms, 165,909 -> 123 allocations
What remains is reading the raw document from storage and hashing it.
@montehurd montehurd closed this Oct 3, 2026
@montehurd montehurd reopened this Oct 3, 2026
@montehurd

Copy link
Copy Markdown
Owner Author

CI passed on all jobs; closing as planned. The change is going upstream.

@montehurd montehurd closed this Oct 3, 2026
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