125 lines
3.9 KiB
Go
125 lines
3.9 KiB
Go
// Package postgres opens the connection pool and applies the schema.
|
|
//
|
|
// Pool size and server-side timeouts are constants rather than settings: they
|
|
// bound how long one request can hold a connection, and the request deadline
|
|
// of the API is chosen against them.
|
|
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/pressly/goose/v3"
|
|
"github.com/pressly/goose/v3/lock"
|
|
|
|
"messenger/internal/platform/apperr"
|
|
"messenger/migrations"
|
|
)
|
|
|
|
const (
|
|
maxConns = 10
|
|
connectTimeout = 3 * time.Second
|
|
statementTimeout = 5 * time.Second
|
|
idleInTxTimeout = 10 * time.Second
|
|
)
|
|
|
|
const codeUniqueViolation = "23505"
|
|
|
|
func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
|
|
cfg, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse database url: %w", err)
|
|
}
|
|
cfg.MaxConns = maxConns
|
|
cfg.ConnConfig.ConnectTimeout = connectTimeout
|
|
cfg.ConnConfig.RuntimeParams["statement_timeout"] = strconv.FormatInt(statementTimeout.Milliseconds(), 10)
|
|
cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = strconv.FormatInt(idleInTxTimeout.Milliseconds(), 10)
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create pool: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("ping database: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|
|
|
|
// Migrate applies pending migrations and returns the versions it applied. A
|
|
// session advisory lock serialises concurrent starts, so several API
|
|
// replicas may call it at once; applied versions are recorded, which makes a
|
|
// repeated run a no-op.
|
|
func Migrate(ctx context.Context, pool *pgxpool.Pool) ([]int64, error) {
|
|
db := stdlib.OpenDBFromPool(pool)
|
|
defer db.Close()
|
|
locker, err := lock.NewPostgresSessionLocker()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("migration lock: %w", err)
|
|
}
|
|
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.FS, goose.WithSessionLocker(locker))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("load migrations: %w", err)
|
|
}
|
|
results, err := provider.Up(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("apply migrations: %w", err)
|
|
}
|
|
applied := make([]int64, 0, len(results))
|
|
for _, r := range results {
|
|
applied = append(applied, r.Source.Version)
|
|
}
|
|
return applied, nil
|
|
}
|
|
|
|
// Classify marks a failure to reach the database as apperr.KindUnavailable,
|
|
// which the API answers with 503 instead of 500; any other error is returned
|
|
// unchanged. Unreachable means: the connection could not be made or broke,
|
|
// the request deadline passed, or the server refused work (PostgreSQL error
|
|
// classes 08 connection exception, 53 insufficient resources, 57 operator
|
|
// intervention such as shutdown or statement timeout).
|
|
func Classify(err error) error {
|
|
if err == nil || !unreachable(err) {
|
|
return err
|
|
}
|
|
return apperr.Wrap(apperr.KindUnavailable, "database unavailable", err)
|
|
}
|
|
|
|
func unreachable(err error) bool {
|
|
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
|
switch class := pgErr.Code[:min(2, len(pgErr.Code))]; class {
|
|
case "08", "53", "57":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
if _, ok := errors.AsType[*pgconn.ConnectError](err); ok {
|
|
return true
|
|
}
|
|
if _, ok := errors.AsType[net.Error](err); ok {
|
|
return true
|
|
}
|
|
return errors.Is(err, context.DeadlineExceeded) ||
|
|
errors.Is(err, context.Canceled) ||
|
|
errors.Is(err, io.EOF) ||
|
|
errors.Is(err, io.ErrUnexpectedEOF) ||
|
|
errors.Is(err, net.ErrClosed) ||
|
|
pgconn.Timeout(err) ||
|
|
pgconn.SafeToRetry(err)
|
|
}
|
|
|
|
// IsUniqueViolation reports whether err is a unique violation of the named
|
|
// constraint.
|
|
func IsUniqueViolation(err error, constraint string) bool {
|
|
pgErr, ok := errors.AsType[*pgconn.PgError](err)
|
|
return ok && pgErr.Code == codeUniqueViolation && pgErr.ConstraintName == constraint
|
|
}
|