audio streaming app plyr.fm
38
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf: warm full connection pool at startup, not just one connection (#1287)

the pool warmup (added in #1025) only opened a single connection.
with pool_size=10, the other 9 connections still hit TCP+SSL setup
on the first burst of requests after deploy. logfire traces show 17
simultaneous connect events taking 1.5-5.5s each during deploys,
causing simple PK lookups to take 12s+ while connections queue up.

fix: warm all pool_size connections concurrently at startup using
asyncio.gather. connections execute SELECT 1 then return to the pool
ready for immediate use. partial failures are logged but don't block
startup.

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

authored by

nate nowack
Claude Opus 4.6 (1M context)
and committed by
GitHub
aa939af2 ea0ddb9b

+22 -4
+22 -4
backend/src/backend/main.py
··· 69 69 await queue_service.setup() 70 70 await jam_service.setup() 71 71 72 - # warm the database connection pool so the first request avoids cold connect 72 + # warm the full database connection pool so deploy doesn't cause a 73 + # connection storm — open pool_size connections concurrently, each 74 + # executes SELECT 1 then returns to the pool ready for use. 73 75 try: 74 76 engine = get_engine() 75 - async with engine.connect() as conn: 76 - await conn.execute(text("SELECT 1")) 77 - logger.info("database connection pool warmed") 77 + pool_size = settings.database.pool_size 78 + 79 + async def _warm_one() -> bool: 80 + async with engine.connect() as conn: 81 + await conn.execute(text("SELECT 1")) 82 + return True 83 + 84 + results = await asyncio.gather( 85 + *[_warm_one() for _ in range(pool_size)], 86 + return_exceptions=True, 87 + ) 88 + warmed = sum(1 for r in results if r is True) 89 + failed = pool_size - warmed 90 + if failed: 91 + logger.warning( 92 + "warmed %d/%d pool connections (%d failed)", warmed, pool_size, failed 93 + ) 94 + else: 95 + logger.info("database connection pool warmed (%d connections)", warmed) 78 96 except (OSError, SQLAlchemyError): 79 97 logger.warning("failed to warm database connection pool") 80 98