perf(api): stop rebuilding proxy catalogues, vaults and cdms per request

Cache proxy provider catalogues (10 min), reuse vaults and CDMs across one batch licence call, and cache the health update check, /api/services entries and parsed service config.yaml.
This commit is contained in:
imSp4rky
2026-11-02 14:40:58 -07:00
parent e01204ac13
commit bb081046e2
23 changed files with 316 additions and 163 deletions
+37
View File
@@ -0,0 +1,37 @@
"""The proxy provider catalogue cache."""
from __future__ import annotations
import pytest
from unshackle.core.proxies import proxy
from unshackle.core.proxies.proxy import cached_catalogue
def test_catalogue_is_fetched_once_until_it_expires(monkeypatch: pytest.MonkeyPatch) -> None:
calls: list[int] = []
@cached_catalogue
def fetch() -> list[dict]:
calls.append(1)
return [{"country": "CA"}]
assert fetch() == fetch() == [{"country": "CA"}]
assert len(calls) == 1
monkeypatch.setattr(proxy, "CATALOGUE_TTL", 0.0)
fetch()
assert len(calls) == 2
def test_empty_catalogue_is_not_kept() -> None:
calls: list[int] = []
@cached_catalogue
def fetch() -> list[dict]:
calls.append(1)
return []
fetch()
fetch()
assert len(calls) == 2
+17
View File
@@ -66,3 +66,20 @@ def test_server_pushes_again_after_one_vault_failed(monkeypatch: pytest.MonkeyPa
handlers.cache_to_vaults({KID.hex: KEY}, "SVC")
handlers.cache_to_vaults({KID.hex: KEY}, "SVC")
assert len(pushes) == 2
def test_one_licence_request_builds_the_server_vaults_once(monkeypatch: pytest.MonkeyPatch) -> None:
builds: list = []
pushes: list = []
def build(name: str) -> Any:
builds.append(name)
return fake_server_vaults(pushes, [FakeVault(False)])
monkeypatch.setattr(handlers, "load_server_vaults", build)
monkeypatch.setattr(handlers, "CACHED_PAIRS", set())
shared: dict = {}
handlers.cache_to_vaults({KID.hex: KEY}, "SVC", shared=shared)
handlers.cache_to_vaults({KID.hex: KEY}, "SVC", shared=shared)
assert len(builds) == 1
assert len(pushes) == 2