From e90fb0774c05915733efcc2456149bb459b5efba Mon Sep 17 00:00:00 2001 From: YueGuobin Date: Mon, 15 Jun 2026 23:26:28 +0800 Subject: [PATCH] Warm up database connection pool on startup to avoid 8s cold-start penalty on first API request --- gns3server/db/tasks.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gns3server/db/tasks.py b/gns3server/db/tasks.py index 981106bcd..06816d584 100644 --- a/gns3server/db/tasks.py +++ b/gns3server/db/tasks.py @@ -99,6 +99,14 @@ async def connect_to_db(app: FastAPI) -> None: return row[0] if row else "unknown" wal_mode = await _verify_conn.run_sync(_check_wal) log.info(f"SQLite journal mode: {wal_mode} {'✅' if wal_mode and wal_mode.upper() == 'WAL' else '❌ will cause database contention'}") + # Warm up the connection pool and ORM cache so the first API request + # doesn't pay the cold-start penalty (observed: 6-8s first DB query) + async with engine.connect() as _warmup: + def _warmup_query(conn): + from sqlalchemy import text + conn.execute(text("SELECT 1")) + await _warmup.run_sync(_warmup_query) + log.info("Database connection pool warmed up") alembic_cfg = config.Config() alembic_cfg.set_main_option("script_location", "gns3server:db_migrations") #alembic_cfg.set_main_option('sqlalchemy.url', db_url)