Files
romm/backend/tests/test_web_server_config.py
Sam Dornan b91a1af13e perf(nginx): drop open_file_cache and guard the keepalive precondition
open_file_cache keyed descriptors by path, so a cover replaced via
os.replace() kept serving the old inode until the validity window expired,
and the ?ts= buster does not change the filesystem key. The comment beside
it claimed the opposite. The benefit was never measured either: the cache
saves an open() and an fstat() per request, which is microseconds on a
local file, and covers are served immutable so repeat hits are rare.
Removing it is simpler than scoping it away from the one tree it was
supposed to help.

The keepalive test asserted gunicorn outlives nginx's idle timeout without
checking that nginx pools upstream connections at all, which it only does
by default from 1.29.7, so a downgrade would have left the test green and
the pooling silently gone. It now checks the pinned version too.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HDXxq3vAxJVGhm1pQuK8V7
2026-09-12 01:44:25 +00:00

68 lines
2.5 KiB
Python

"""Guards the serving-layer settings that live outside Python."""
import re
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
NGINX_CONF = REPO_ROOT / "docker" / "nginx" / "default.conf"
DOCKERFILE = REPO_ROOT / "docker" / "Dockerfile"
INIT_SCRIPT = REPO_ROOT / "docker" / "init_scripts" / "init"
ENV_TEMPLATE = REPO_ROOT / "env.template"
# nginx's own default upstream keepalive_timeout, which gunicorn must outlive.
NGINX_UPSTREAM_IDLE_TIMEOUT = 60
# nginx enables upstream keepalive by default from this release. Below it the
# pool is off entirely and the timeout invariant stops meaning anything.
NGINX_UPSTREAM_KEEPALIVE_SINCE = (1, 29, 7)
REQUIRED_GZIP_TYPES = [
"application/wasm", # EmulatorJS, js-dos and FAKE-08 cores
"image/svg+xml", # player logos and favicons
"application/json",
"application/javascript",
"text/css",
]
def _init_script_keepalive() -> int:
match = re.search(
r'--keep-alive "\$\{WEB_SERVER_KEEPALIVE:-(\d+)\}"', INIT_SCRIPT.read_text()
)
assert match, "could not read the gunicorn --keep-alive default"
return int(match.group(1))
@pytest.fixture(scope="module")
def gzip_types() -> set[str]:
match = re.search(r"\bgzip_types\s+([^;]+);", NGINX_CONF.read_text())
assert match, "no gzip_types directive in the nginx config"
return set(match.group(1).split())
@pytest.mark.parametrize("media_type", REQUIRED_GZIP_TYPES)
def test_compressible_types_are_gzipped(gzip_types: set[str], media_type: str) -> None:
assert media_type in gzip_types, f"{media_type} would be served uncompressed"
def test_nginx_defaults_upstream_keepalive_on() -> None:
match = re.search(r"^ARG NGINX_VERSION=(\S+)", DOCKERFILE.read_text(), re.M)
assert match, "docker/Dockerfile is missing an ARG NGINX_VERSION pin"
version = tuple(int(part) for part in match.group(1).split("."))
assert version >= NGINX_UPSTREAM_KEEPALIVE_SINCE, (
f"nginx {match.group(1)} does not pool upstream connections by default, "
f"so every API request opens a new one"
)
def test_gunicorn_outlives_nginx_upstream_idle_timeout() -> None:
assert _init_script_keepalive() > NGINX_UPSTREAM_IDLE_TIMEOUT
def test_env_template_documents_the_same_keepalive_default() -> None:
match = re.search(r"^WEB_SERVER_KEEPALIVE=(\d+)", ENV_TEMPLATE.read_text(), re.M)
assert match, "env.template is missing WEB_SERVER_KEEPALIVE"
assert int(match.group(1)) == _init_script_keepalive()