Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/willnewby/qq. A fast, lightweight job queue based on Postgres, Go and Bash
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455package database
import ( "context" "fmt"
"github.com/jackc/pgx/v5/pgxpool")
// DB represents a PostgreSQL database connection pooltype DB struct { Pool *pgxpool.Pool}
// New creates a new database connection poolfunc 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 poolfunc (db *DB) Close() { if db.Pool != nil { db.Pool.Close() }}