mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-14 21:15:06 +00:00
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:
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user