Boiler-Go uses pgxpool to manage PostgreSQL database connections efficiently. The connection pool is initialized once at startup and shared across the application.Documentation Index
Fetch the complete documentation index at: https://mnah05-boiler-go-21-79.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Architecture
The database pool is implemented as a singleton with thread-safe access usingsync.RWMutex. This ensures:
- Single initialization per application lifecycle
- Safe concurrent access from multiple goroutines
- Clean shutdown with proper resource cleanup
Pool configuration
The pool is configured ininternal/db/pool.go:28-36 with production-ready defaults:
Configuration parameters explained
Configuration parameters explained
Maximum number of concurrent connections. Prevents database overload.
Minimum connections kept alive. Reduces latency for incoming requests.
Maximum time a connection can be reused before being closed and recreated.
Maximum time an idle connection stays open before being closed.
Interval between automatic health checks on idle connections.
Initialization
The pool is initialized in bothcmd/api/main.go and cmd/worker/main.go with a timeout context:
Usage
Access the pool anywhere in your application using the thread-safe getter:Example query
Shutdown
The pool is automatically closed during graceful shutdown:- All active connections are properly closed
- Resources are released back to the OS
- No connection leaks occur
Error handling
The initialization process includes comprehensive error handling:The pool automatically cleans itself up if the ping fails, preventing resource leaks even during initialization failures.
Best practices
- Always use context: Pass a context with appropriate timeout to all queries
- Connection limit: Keep
MaxConnsaligned with your database’smax_connectionssetting - Monitor metrics: Use
pool.Stat()to track connection pool health - Avoid connection exhaustion: Ensure all queries complete within reasonable timeframes