Files
romm/backend/tests/test_nginx_cache_headers.py
Georges-Antoine Assi ec1684d2af perf(metadata): serve the metadata dumps and fixtures from SQLite files (#5844)
* perf(metadata): serve the metadata dumps and fixtures from SQLite files

The LaunchBox and Switch TitleDB stores sat in valkey, which keeps every
byte resident: about 300MB on an instance with both imported, against
roughly 1MB for an empty server. Every read is an exact-key lookup and
nothing writes between imports, so they now live in read-only SQLite
files under RESOURCES_BASE_PATH/metadata, where the OS page cache holds
only the pages lookups touch.

A build writes a temporary file and renames it over the previous one,
so readers see the old store or the new one and never a partial import.
That replaces the initial-import flag and the schema stamp: the store
revision is part of the file name. A failed import keeps serving the
previous file.

The startup fixture indexes (MAME, ScummVM, Sony serials, known BIOS)
no longer get copied into valkey on every boot. They build on first use
into a file under the cache path, named after a hash of the JSON.

Startup deletes the old valkey keys and any file of an older revision,
and queues a rebuild when a store had data, so an upgrade refills
LaunchBox and TitleDB on its own. nginx returns 404 for the metadata
folder, since /assets/romm/resources is otherwise served without auth.

Measured against the real dumps: LaunchBox builds in 22s into 263MB on
disk, TitleDB in 1.2s into 54MB, the fixtures in 0.2s into 8MB. A hit
costs 10-23us including decompression, a miss about 7us.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review fixes)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep the previous metadata store when a dump comes out empty

A LaunchBox or TitleDB build that writes no rows to its main table now
fails with EmptyDumpError instead of swapping an empty store over a
populated one.

Each store keeps one connection per process behind a lock instead of one
per thread. A build closes it once it swaps the file in, and any other
process closes its own on its next lookup, so a replaced file's disk
space is released instead of held by idle threads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Address Greptile review: trim DumpStore comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Restore valkey's own snapshot policy by default

The hourly default (48259b3a9, #3980) kept valkey from rewriting the
hundreds of MB of LaunchBox and TitleDB data it held. With those moved to
SQLite files, a snapshot holds only sessions, queues and query caches, so
an unset REDIS_SAVE_POLICY now leaves valkey's stock policy in place and
loses less on a crash. An empty value still disables snapshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 08:49:15 -05:00

302 lines
12 KiB
Python

"""Guards the asset caching policy in the nginx template (issue #3890).
The Docker image's nginx serves the whole frontend. Assets fall into caching
classes and the template must sort each request into the right one:
* Frozen forever (fingerprinted): content-hashed Vite bundles
(``index-<hash>.js``) and library resources requested with a cache-busting
query (covers ``?ts=``, manuals ``?v=``). The URL changes when the file
does, so a cached copy is never stale.
* Revalidating (stable URLs across image upgrades / re-scrapes): the bundled
EmulatorJS/Ruffle runtimes, icons/fonts/logos, and the query-less resources
(screenshots, RA badges, which reuse names like ``0.jpg`` and are
overwritten in place).
* ``no-cache`` (must always be fresh): ``index.html``, the service worker,
and the root favicons/manifest.
The resources bucket splits on whether a recognized version param (``ts``/``v``)
is present, via the ``$resources_cache_control`` map (an unrelated query like
``?download=1`` must not freeze a stable file). Rather than grepping the
template for a string, this parses the ``location`` and ``map`` blocks and
replays nginx's matching for representative URLs, asserting the
``Cache-Control`` each resolves to.
"""
import re
from pathlib import Path
import pytest
NGINX_TEMPLATE = (
Path(__file__).resolve().parents[2]
/ "docker"
/ "nginx"
/ "templates"
/ "default.conf.template"
)
IMMUTABLE_MAX_AGE = 31536000 # one year, in seconds
# Any revalidating cache must stay well under this so upgrades/re-scrapes show.
MAX_REVALIDATING_AGE = 86400 # one day, in seconds
def _read_block(text: str, start: int) -> tuple[str, int]:
"""Return (body, end) for a brace block whose opening ``{`` is at start-1,
scanning to the matching ``}``. Blocks in this template do not nest."""
depth = 1
i = start
while i < len(text) and depth:
if text[i] == "{":
depth += 1
elif text[i] == "}":
depth -= 1
i += 1
return text[start : i - 1], i
class Location:
"""A parsed nginx ``location``: modifier, match, and the Cache-Control it
sets (a quoted literal, or a ``$variable`` resolved through a map)."""
def __init__(self, modifier: str, match: str, body: str):
self.modifier = modifier
self.match = match
self.body = body
cache = re.search(
r'add_header\s+Cache-Control\s+(?:"([^"]*)"|(\$[A-Za-z0-9_]+))',
body,
re.IGNORECASE,
)
self.cache_control = (
(cache.group(1) if cache.group(1) is not None else cache.group(2))
if cache
else None
)
@property
def is_regex(self) -> bool:
return self.modifier in ("~", "~*")
@property
def is_prefix(self) -> bool:
return self.modifier in ("", "^~")
def _parse_locations(text: str) -> list[Location]:
"""Extract every ``location`` block, preserving file order (nginx evaluates
regex locations in the order they appear)."""
locations: list[Location] = []
header = re.compile(r'location\s+(=|\^~|~\*|~)?\s*(?:"([^"]+)"|(\S+))\s*\{')
for m in header.finditer(text):
modifier = m.group(1) or ""
match = m.group(2) if m.group(2) is not None else m.group(3)
body, _ = _read_block(text, m.end())
locations.append(Location(modifier, match, body))
return locations
def _parse_maps(text: str) -> dict[str, tuple[str, dict[str, str]]]:
"""Parse ``map $in $out { key value; ... }`` -> {out: ($in, {key: value})}."""
maps: dict[str, tuple[str, dict[str, str]]] = {}
entry = re.compile(r'(?:"([^"]*)"|(\S+))\s+(?:"([^"]*)"|(\S+))\s*;')
for m in re.finditer(r"map\s+(\$[A-Za-z0-9_]+)\s+\$([A-Za-z0-9_]+)\s*\{", text):
input_var, output_var = m.group(1), m.group(2)
body, _ = _read_block(text, m.end())
entries: dict[str, str] = {}
for e in entry.finditer(body):
key = e.group(1) if e.group(1) is not None else e.group(2)
value = e.group(3) if e.group(3) is not None else e.group(4)
entries[key] = value
maps[output_var] = (input_var, entries)
return maps
def _input_value(input_var: str, url: str) -> str:
"""The nginx source a map keys on. Only ``$args`` is used here."""
query = url.split("?", 1)[1] if "?" in url else ""
if input_var == "$args":
return query
if input_var == "$is_args":
return "?" if query else ""
raise NotImplementedError(f"map input {input_var} not modelled")
def _eval_map(input_var: str, entries: dict[str, str], url: str) -> str | None:
"""Resolve an nginx ``map`` for ``url``, mirroring nginx precedence: exact
keys first, then regex keys (``~``/``~*``) in order, else ``default``."""
value = _input_value(input_var, url)
for key, out in entries.items():
if key != "default" and not key.startswith("~") and key == value:
return out
for key, out in entries.items():
if key.startswith("~"):
pattern = key[1:]
flags = re.IGNORECASE if pattern.startswith("*") else 0
if re.search(pattern.lstrip("*"), value, flags):
return out
return entries.get("default")
def _resolve(locations: list[Location], path: str) -> Location | None:
"""Replay nginx's location selection for ``path`` (query stripped): exact,
then longest prefix, then first matching regex, else that prefix."""
for loc in locations:
if loc.modifier == "=" and loc.match == path:
return loc
best_prefix: Location | None = None
for loc in locations:
if loc.is_prefix and path.startswith(loc.match):
if best_prefix is None or len(loc.match) > len(best_prefix.match):
best_prefix = loc
if best_prefix is not None and best_prefix.modifier == "^~":
return best_prefix
for loc in locations:
if loc.is_regex:
flags = re.IGNORECASE if loc.modifier == "~*" else 0
if re.search(loc.match, path, flags):
return loc
return best_prefix
@pytest.fixture(scope="module")
def config() -> tuple[list[Location], dict[str, tuple[str, dict[str, str]]]]:
text = NGINX_TEMPLATE.read_text()
return _parse_locations(text), _parse_maps(text)
def _cache_control_for(config, url: str) -> str | None:
locations, maps = config
loc = _resolve(locations, url.split("?", 1)[0])
assert loc is not None, f"no location matched {url}"
cc = loc.cache_control
if cc is not None and cc.startswith("$"):
input_var, entries = maps[cc[1:]]
cc = _eval_map(input_var, entries, url)
return cc
# Fingerprinted -> safe to cache immutably forever. Library resources qualify
# only when they carry a cache-busting query (covers ?ts=, manuals ?v=).
IMMUTABLE_URLS = [
"/assets/index-C-Cea5tF.js",
"/assets/index-C-Cea5tF.css",
"/assets/EmulatorJS--ZSfrbCp.js", # hashed vite chunk with hyphens in name
"/assets/emulatorjs-logotype-COnXko2R.svg",
"/assets/inter-latin-Ckm5tF12.woff2",
"/assets/romm/resources/roms/1/1/cover/small.png?ts=2026-01-01T00:00:00",
"/assets/romm/resources/roms/1/1/cover/big.png?ts=1700000000",
"/assets/romm/resources/roms/1/1/manual/1.pdf?v=1700000000",
]
# Stable URLs that must revalidate so an upgrade / re-scrape is picked up. This
# includes query-less resources: screenshots and RA badges reuse fixed names
# and are overwritten in place, so they are NOT safe to freeze.
REVALIDATING_URLS = [
"/assets/emulatorjs/data/loader.js", # bundled EmulatorJS runtime
"/assets/emulatorjs/data/cores/snes9x-wasm.data",
"/assets/ruffle/ruffle.js",
"/assets/scrappers/ss.png",
"/assets/platforms/default.ico",
"/assets/isotipo.png", # non-hashed file directly under /assets
"/assets/patcherjs/patcherjs.png",
"/assets/romm/resources/roms/1/1/screenshots/0.jpg", # overwritten on re-scrape
"/assets/romm/resources/roms/1/1/screenshots/1.jpg",
"/assets/romm/resources/roms/1/1/ra/badges/12345.png", # RA badge, no query
# An unrelated query must NOT be mistaken for a version bust and frozen.
"/assets/romm/resources/roms/1/1/screenshots/0.jpg?download=1",
]
# Served from ``location /`` (not /assets): index.html, the service worker and
# the favicons/manifest all keep stable names, so they must revalidate rather
# than be frozen. A stale index.html could point at purged, hashed bundles.
ROOT_REVALIDATING_URLS = [
"/",
"/index.html",
"/library", # SPA route -> index.html
"/favicon.ico",
"/favicon.svg",
"/manifest.webmanifest",
"/sw.js",
"/apple-touch-icon.png",
]
@pytest.mark.parametrize("url", IMMUTABLE_URLS)
def test_fingerprinted_assets_are_immutable(config, url):
cc = _cache_control_for(config, url)
assert cc is not None, f"{url} has no Cache-Control"
assert "immutable" in cc, f"{url} should be immutable, got: {cc!r}"
max_age = re.search(r"max-age=(\d+)", cc)
assert (
max_age and int(max_age.group(1)) == IMMUTABLE_MAX_AGE
), f"{url} should cache for a year, got: {cc!r}"
@pytest.mark.parametrize("url", REVALIDATING_URLS)
def test_non_fingerprinted_assets_revalidate(config, url):
cc = _cache_control_for(config, url)
assert cc is not None, f"{url} should still be cacheable, got no Cache-Control"
assert (
"immutable" not in cc
), f"{url} keeps a stable URL across upgrades and must not be immutable: {cc!r}"
assert (
"must-revalidate" in cc
), f"{url} must revalidate so upgrades/re-scrapes propagate, got: {cc!r}"
max_age = re.search(r"max-age=(\d+)", cc)
assert (
max_age and int(max_age.group(1)) <= MAX_REVALIDATING_AGE
), f"{url} cache window is too long to catch an upgrade: {cc!r}"
def test_resources_freeze_only_when_cache_busted(config):
# Same resource tree: only a recognized version param (ts/v) freezes a
# resource. A stable name with no query, or with an unrelated query, must
# revalidate. This is why the resources bucket can't be a blanket immutable
# rule, and why it keys on the param name rather than on "any query".
cover = _cache_control_for(
config, "/assets/romm/resources/roms/1/1/cover/small.png?ts=123"
)
screenshot = _cache_control_for(
config, "/assets/romm/resources/roms/1/1/screenshots/0.jpg"
)
unrelated = _cache_control_for(
config, "/assets/romm/resources/roms/1/1/screenshots/0.jpg?download=1"
)
assert cover is not None and screenshot is not None and unrelated is not None
assert "immutable" in cover, f"cover should freeze, got: {cover!r}"
assert (
"immutable" not in screenshot and "must-revalidate" in screenshot
), f"queryless screenshot must revalidate, got: {screenshot!r}"
assert (
"immutable" not in unrelated and "must-revalidate" in unrelated
), f"unrelated ?download= must not freeze, got: {unrelated!r}"
@pytest.mark.parametrize("url", ROOT_REVALIDATING_URLS)
def test_root_files_revalidate(config, url):
cc = _cache_control_for(config, url)
assert cc is not None, f"{url} should carry a revalidating Cache-Control"
assert "immutable" not in cc, f"{url} must not be immutable, got: {cc!r}"
assert (
"no-cache" in cc or "must-revalidate" in cc
), f"{url} must revalidate so deploys/updates propagate, got: {cc!r}"
@pytest.mark.parametrize(
"url",
[
"/assets/romm/resources/metadata/launchbox.v1.sqlite",
"/assets/romm/resources/metadata/.launchbox.v1.sqlite.abc.tmp",
# A case-insensitive volume would serve the file under any casing.
"/assets/romm/resources/Metadata/launchbox.v1.sqlite",
],
)
def test_metadata_dumps_are_never_served(config, url):
locations, _ = config
loc = _resolve(locations, url)
assert loc is not None and re.search(r"\breturn\s+404\b", loc.body), url