34 lines
836 B
Go
34 lines
836 B
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// NewPool creates a pgxpool with sensible defaults for ingest workloads.
|
|
// Pool size is small because ingest is mostly cache hits — Postgres is only
|
|
// touched on cache miss (write key lookup, schema upsert).
|
|
func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
|
|
cfg, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse pg dsn: %w", err)
|
|
}
|
|
cfg.MaxConns = 16
|
|
cfg.MinConns = 2
|
|
cfg.MaxConnIdleTime = 5 * time.Minute
|
|
cfg.HealthCheckPeriod = 30 * time.Second
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pg connect: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("pg ping: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|