Repository navigation
Use a connection pool instead of a single DB connection - #408
Conversation
Signed-off-by: Radoslav Dimitrov <[email protected]>
Signed-off-by: Radoslav Dimitrov <[email protected]>
There was a problem hiding this comment.
Pull Request Overview
This PR replaces the single PostgreSQL database connection with a connection pool to resolve "conn busy" issues occurring during concurrent requests. The change migrates from using *pgx.Conn to *pgxpool.Pool to better handle multiple simultaneous database operations.
- Replaces single database connection with a connection pool configuration
- Updates all database operations to use the pool instead of direct connection
- Adds connection pool settings optimized for stability and concurrent load handling
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/database/postgres.go | Migrates from single connection to connection pool with configured settings and updates all database operations |
| go.mod | Adds required dependencies for connection pooling (puddle/v2 and golang.org/x/sync) |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
@claude - please review this PR while making sure it addresses the referenced issues edit: I assume it fails because I'm using a fork instead of a branch in origin |
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
Review from Claude Code locallyCode Review for PR #408: Use a connection pool instead of a single DB connectionThis PR addresses a critical production issue where concurrent requests to the PostgreSQL database were failing with "conn busy" errors. The solution correctly migrates from using a single database connection ( ✅ Strengths
🔧 Suggestions for Improvement
The current migration setup could be more robust: // Current code: migrator := NewMigrator(conn.Conn()) Suggestion: Add cleanup in case migration fails: conn, err := pool.Acquire(ctx) migrator := NewMigrator(conn.Conn())
The hardcoded pool settings might not be optimal for all deployment environments: // Consider adding to config struct:
Consider adding a timeout for pool creation to prevent hanging during startup: // Create connection pool with configured settings pool, err := pgxpool.NewWithConfig(ctx, config)
For production observability, consider exposing pool stats: // Add method to expose pool stats for monitoring 🧪 Testing Recommendations The PR mentions "Builds locally" but doesn't include tests for the pool functionality. Consider adding:
🔍 Code Quality
✅ Overall Assessment This is a solid fix for a critical production issue. The approach is sound and the implementation is clean. The suggested improvements are mostly about robustness and configurability rather than correctness. Recommendation: ✅ Approve with minor suggestions The core fix is correct and will resolve the immediate "conn busy" issues in production. The suggestions above can be addressed in this PR or follow-up PRs depending on urgency. I don't think its suggestions are needed. Maybe the observability bit would be nice, so we know if we are butting up against the limits regularly, but can fast follow if we want that. Thank you for fixing!! |
domdomegg
left a comment
There was a problem hiding this comment.
ty, happy to approve this for now
In future would it be better to use pgbouncer so if we scale replicas etc. the connections don't also vary? I think CloudNative Postgres, what we use for hosting the database, supports this fairly easily: https://cloudnative-pg.io/documentation/1.16/connection_pooling/
Motivation and Context
The following PR should fix the recent "conn busy" issue caused by the PostgreSQL implementation using a single database connection (*pgx.Conn) instead of a connection pool (*pgxpool.Pool). When multiple concurrent requests try to access the endpoint, they would conflict trying to use the same connection.
How Has This Been Tested?
Builds locally
Breaking Changes
No
Types of changes
Checklist
Additional context
Related to #400, #405 and #407