31 lines
713 B
Go
31 lines
713 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func Open(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
|
poolConfig, err := pgxpool.ParseConfig(databaseURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parsing database configuration: %w", err)
|
|
}
|
|
poolConfig.MaxConns = 12
|
|
poolConfig.MinConns = 1
|
|
poolConfig.MaxConnLifetime = 30 * time.Minute
|
|
poolConfig.MaxConnIdleTime = 5 * time.Minute
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("opening database: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("pinging database: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|