Skip to content

Add connection health checks and recycling to psycopg pool#42

Merged
MattiaFailla merged 2 commits intomasterfrom
copilot/improve-database-connection
Feb 23, 2026
Merged

Add connection health checks and recycling to psycopg pool#42
MattiaFailla merged 2 commits intomasterfrom
copilot/improve-database-connection

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 23, 2026

NeonDB free tier aggressively closes idle connections, causing SSL connection has been closed unexpectedly errors when the pool hands out stale connections.

  • check=AsyncConnectionPool.check_connection — pre-ping connections before use, discarding dead ones
  • max_lifetime=300 — recycle connections after 5 minutes to avoid server-side timeouts
pool = AsyncConnectionPool(
    database_url,
    open=False,
    check=AsyncConnectionPool.check_connection,
    max_lifetime=300,
)

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

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>
Copilot AI changed the title [WIP] Improve database connection handling with pre-ping Add connection health checks and recycling to psycopg pool Feb 23, 2026
Copilot AI requested a review from MattiaFailla February 23, 2026 12:11
@MattiaFailla MattiaFailla marked this pull request as ready for review February 23, 2026 12:11
@MattiaFailla MattiaFailla merged commit 0e02299 into master Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants