import time from contextlib import contextmanager from pathlib import Path import psycopg from psycopg.rows import dict_row from psycopg.types.json import Jsonb from psycopg_pool import ConnectionPool, PoolTimeout from app import telemetry from app.errors import AppError, unavailable from app.faults import db_fail, db_slow_seconds from app.settings import settings pool = None def init(): global pool pool = ConnectionPool( settings.database_url, min_size=1, max_size=settings.pool_max, timeout=2, max_waiting=16, check=ConnectionPool.check_connection, kwargs={ "row_factory": dict_row, "autocommit": False, "connect_timeout": 3, "options": "-c timezone=UTC -c statement_timeout=4000", }, open=True, reconnect_timeout=2, ) def migrate(): script = Path(__file__).with_name("schema.sql").read_text(encoding="utf-8") statements = [] buffer = [] for line in script.splitlines(): stripped = line.strip() if not stripped or stripped.startswith("--"): continue buffer.append(line) if stripped.endswith(";"): statements.append("\n".join(buffer)) buffer = [] with pool.connection() as conn: conn.execute("SELECT pg_advisory_lock(861006)") for statement in statements: conn.execute(statement) conn.execute("SELECT pg_advisory_unlock(861006)") conn.commit() def jsonb(value): return Jsonb(value) @contextmanager def connection(): if db_fail(): raise unavailable() delay = db_slow_seconds() started = time.perf_counter() span = telemetry.tracer().start_as_current_span( "db.transaction", attributes={"db.system": "postgresql"}, ) span.__enter__() try: if delay: time.sleep(delay) with pool.connection() as conn: try: yield conn conn.commit() except Exception: conn.rollback() raise except AppError: raise except (PoolTimeout, psycopg.OperationalError, psycopg.InterfaceError) as exc: raise unavailable() from exc finally: span.__exit__(None, None, None) telemetry.record_db(time.perf_counter() - started) def stats(): if pool is None: return 0, 0 data = pool.get_stats() used = int(data.get("pool_size", 0)) - int(data.get("pool_available", 0)) waiting = int(data.get("requests_waiting", 0)) return max(used, 0), waiting