package database import ( "context" "fmt" "github.com/jackc/pgx/v5/pgxpool" ) // DB represents a PostgreSQL database connection pool type DB struct { Pool *pgxpool.Pool } // New creates a new database connection pool func New(ctx context.Context, dbURL string) (*DB, error) { if dbURL == "" { return nil, fmt.Errorf("database URL is required") } config, err := pgxpool.ParseConfig(dbURL) if err != nil { return nil, fmt.Errorf("failed to parse database URL: %w", err) } // Limit pool size to prevent overloading the database. // pgxpool defaults to runtime.NumCPU which can be too high. if config.MaxConns == 0 { config.MaxConns = 10 } if config.MinConns == 0 { config.MinConns = 2 } pool, err := pgxpool.NewWithConfig(ctx, config) if err != nil { return nil, fmt.Errorf("failed to create connection pool: %w", err) } // Verify the connection if err := pool.Ping(ctx); err != nil { pool.Close() return nil, fmt.Errorf("failed to ping database: %w", err) } return &DB{Pool: pool}, nil } // Close closes the database connection pool func (db *DB) Close() { if db.Pool != nil { db.Pool.Close() } }