Skip to content

Commit 5cae909

Browse files
CopilotMattiaFailla
andcommitted
Add pool pre-ping check and max_lifetime=300 to AsyncConnectionPool
Adds check=AsyncConnectionPool.check_connection to detect and discard dead connections before use (equivalent to pool_pre_ping=True), and max_lifetime=300 to recycle connections older than 5 minutes (equivalent to pool_recycle=300). This fixes SSL connection closure errors on NeonDB's free tier. Co-authored-by: MattiaFailla <11872425+MattiaFailla@users.noreply.github.com>
1 parent 095ebb4 commit 5cae909

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/python_italy_bot/db/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ async def create_repository(database_url: str | None) -> AsyncRepository:
2626
if database_url and database_url.startswith("postgresql"):
2727
from psycopg_pool import AsyncConnectionPool
2828

29-
pool = AsyncConnectionPool(database_url, open=False)
29+
pool = AsyncConnectionPool(
30+
database_url,
31+
open=False,
32+
check=AsyncConnectionPool.check_connection,
33+
max_lifetime=300,
34+
)
3035
await pool.open()
3136
return PostgresRepository(pool)
3237
return InMemoryRepository()

0 commit comments

Comments
 (0)