perf(iwooos): cache security control plane reads

This commit is contained in:
ogt
2026-07-11 20:21:19 +08:00
parent 9726703a91
commit c241ab72dc
2 changed files with 63 additions and 17 deletions

View File

@@ -17,6 +17,7 @@ logger = structlog.get_logger(__name__)
_SCHEMA_VERSION = "iwooos_security_asset_control_plane_v1"
_QUERY_TIMEOUT_SECONDS = 6.0
_SOURCE_QUERY_TIMEOUT_SECONDS = 4.0
_CACHE_TTL_SECONDS = 30.0
_DISCOVERY_FRESHNESS_SECONDS = 2 * 60 * 60
_WINDOW_HOURS = 24
@@ -1328,24 +1329,45 @@ async def _read_public_source(
class IwoooSSecurityAssetControlPlaneService:
"""Read aggregate production security state with a bounded DB timeout."""
def __init__(self) -> None:
self._cached_payload: dict[str, Any] | None = None
self._cached_at_monotonic = 0.0
self._refresh_lock = asyncio.Lock()
async def get_snapshot(self) -> dict[str, Any]:
try:
return await asyncio.wait_for(
self._load_snapshot(), timeout=_QUERY_TIMEOUT_SECONDS
)
except TimeoutError:
logger.warning("iwooos_security_asset_control_plane_timeout")
return build_unavailable_iwooos_security_asset_control_plane(
"database_timeout"
)
except Exception as exc: # noqa: BLE001 - public readback must degrade safely
logger.warning(
"iwooos_security_asset_control_plane_unavailable",
error_type=type(exc).__name__,
)
return build_unavailable_iwooos_security_asset_control_plane(
"database_query_failed"
)
loop = asyncio.get_running_loop()
if (
self._cached_payload is not None
and loop.time() - self._cached_at_monotonic < _CACHE_TTL_SECONDS
):
return self._cached_payload
async with self._refresh_lock:
if (
self._cached_payload is not None
and loop.time() - self._cached_at_monotonic < _CACHE_TTL_SECONDS
):
return self._cached_payload
try:
payload = await asyncio.wait_for(
self._load_snapshot(), timeout=_QUERY_TIMEOUT_SECONDS
)
except TimeoutError:
logger.warning("iwooos_security_asset_control_plane_timeout")
payload = build_unavailable_iwooos_security_asset_control_plane(
"database_timeout"
)
except Exception as exc: # noqa: BLE001 - public readback must degrade safely
logger.warning(
"iwooos_security_asset_control_plane_unavailable",
error_type=type(exc).__name__,
)
payload = build_unavailable_iwooos_security_asset_control_plane(
"database_query_failed"
)
self._cached_payload = payload
self._cached_at_monotonic = loop.time()
return payload
async def _load_snapshot(self) -> dict[str, Any]:
source_results = await asyncio.gather(