From d62c957d9a0c8ec6ad2e14dbbb3bda166b2a79a4 Mon Sep 17 00:00:00 2001 From: Tarit Witworrasakul <60599564+wtarit@users.noreply.github.com> Date: Sun, 5 Apr 2026 06:59:46 -0700 Subject: [PATCH] Update connection pooling --- app/database.py | 27 +++++++++++++++++++++++++-- compose.dev.yml | 7 ++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/database.py b/app/database.py index 593fcce..8ccb5fc 100644 --- a/app/database.py +++ b/app/database.py @@ -1,4 +1,6 @@ +import heapq import os +import time from peewee import DatabaseProxy, Model from playhouse.pool import PooledPostgresqlDatabase @@ -18,11 +20,32 @@ def init_db(app): port=int(os.environ.get("DATABASE_PORT", 5432)), user=os.environ.get("DATABASE_USER", "postgres"), password=os.environ.get("DATABASE_PASSWORD", "postgres"), - max_connections=20, + max_connections=int(os.environ.get("DATABASE_MAX_CONNECTIONS", 20)), stale_timeout=300, + timeout=10, ) db.initialize(database) + # Pre-fill the connection pool so startup traffic doesn't thundering-herd PostgreSQL + min_connections = int(os.environ.get("DATABASE_MIN_CONNECTIONS", 10)) + warm_conns = [] + for _ in range(min_connections): + try: + conn = super(PooledPostgresqlDatabase, database)._connect() + ts = time.time() + database._heap_counter += 1 + heapq.heappush(database._connections, (ts, database._heap_counter, conn)) + warm_conns.append(conn) + except Exception: + break + + pid = os.getpid() + app.logger.info( + "Connection pool pre-filled: %d/%d connections ready (worker pid=%d, max=%d)", + len(warm_conns), min_connections, pid, + int(os.environ.get("DATABASE_MAX_CONNECTIONS", 20)), + ) + @app.before_request def _db_connect(): from flask import request @@ -31,7 +54,7 @@ def _db_connect(): return db.connect(reuse_if_open=True) - @app.teardown_appcontext + @app.teardown_request def _db_close(exc): if not db.is_closed(): db.close() diff --git a/compose.dev.yml b/compose.dev.yml index 30ee56a..bae7e90 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -1,12 +1,13 @@ services: db: - image: postgres:18-alpine + image: postgres:18 environment: POSTGRES_DB: hackathon_db POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres + command: ["postgres", "-c", "max_connections=200"] volumes: - - pgdata_dev:/var/lib/postgresql/data + - pgdata_dev:/var/lib/postgresql ports: - "5432:5432" healthcheck: @@ -16,7 +17,7 @@ services: retries: 5 valkey: - image: valkey/valkey:8-alpine + image: valkey/valkey:8 ports: - "6379:6379" healthcheck: