From 3d52e0b79e4bbfc185af5faac2eae35b92cd2ee3 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 9 Jul 2026 12:06:40 +0200 Subject: [PATCH] perf(db): serve presses + downtime-reason texts from the shared reference cache press_settings.presses and settings.downtime_reasons are re-read on every 12s overview poll; route them through the kernel cached_select() TTL cache so they stop consuming a shared-pool checkout each poll. Part of the gateway connection-pool-saturation fix (see fastAPI/agent_docs/db-pool-relief.md). Co-Authored-By: Claude Opus 4.8 (1M context) --- _db.py | 4 ++-- api/routes.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/_db.py b/_db.py index fc3e09b..3c6d5ec 100644 --- a/_db.py +++ b/_db.py @@ -16,6 +16,6 @@ real connection opens only when a query actually executes. """ from __future__ import annotations -from app.db.mariadb import run_select_query # noqa: F401 (kernel SELECT-only proxy) +from app.db.mariadb import run_select_query, cached_select # noqa: F401 (kernel read proxies) -__all__ = ["run_select_query"] +__all__ = ["run_select_query", "cached_select"] diff --git a/api/routes.py b/api/routes.py index 57733fd..7a20c46 100644 --- a/api/routes.py +++ b/api/routes.py @@ -46,7 +46,7 @@ from typing import Any, Optional from fastapi import APIRouter, Query -from .._db import run_select_query +from .._db import run_select_query, cached_select logger = logging.getLogger(__name__) @@ -128,8 +128,11 @@ def _group_carts(rows: list[dict[str, Any]], colors: dict[str, int]) -> "dict[st def _fetch_presses(plant: str) -> list[str]: + # Reference data (admin-edited), re-read on every 12s poll — served from the shared + # reference cache so it stops consuming a pool checkout each poll. try: - rows = run_select_query( + rows = cached_select( + f"tov:presses:{plant}", "SELECT psPress AS press FROM press_settings.presses WHERE psPlant = :plant " "ORDER BY CAST(psPress AS UNSIGNED), psPress", {"plant": plant}, @@ -239,8 +242,10 @@ def _fetch_downtime_reasons(plant: str) -> dict[str, int]: def _fetch_reason_texts() -> dict[int, str]: + # Reference data (admin-edited), full-table read on every poll — served from cache. try: - rows = run_select_query( + rows = cached_select( + "tov:reason_texts", "SELECT id, reason_DE AS de FROM settings.downtime_reasons", {} ) except Exception as e: # noqa: BLE001