Перенесите реализацию ЛР 4 на стенд ЛР 5
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
// Package postgres opens the connection pool and applies the schema.
|
||||
//
|
||||
// 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"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"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 (
|
||||
connectTimeout = time.Second
|
||||
statementTimeout = 5 * time.Second
|
||||
idleInTxTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
const codeUniqueViolation = "23505"
|
||||
|
||||
func NewPool(ctx context.Context, dsn string, maxConns int32) (*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
|
||||
// Unlike net.Dialer, pgx resolves the host before ConnectTimeout starts.
|
||||
// Docker DNS takes seconds to give up on a stopped container: unbounded,
|
||||
// every request would wait out its deadline and log only "context
|
||||
// deadline exceeded" instead of the failed lookup.
|
||||
cfg.ConnConfig.LookupFunc = func(ctx context.Context, host string) ([]string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
|
||||
defer cancel()
|
||||
return net.DefaultResolver.LookupHost(ctx, host)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["statement_timeout"] = strconv.FormatInt(statementTimeout.Milliseconds(), 10)
|
||||
cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = strconv.FormatInt(idleInTxTimeout.Milliseconds(), 10)
|
||||
// The application keeps every time in UTC; pgx would otherwise scan
|
||||
// timestamptz into time.Local.
|
||||
cfg.AfterConnect = func(_ context.Context, conn *pgx.Conn) error {
|
||||
conn.TypeMap().RegisterType(&pgtype.Type{
|
||||
Name: "timestamptz",
|
||||
OID: pgtype.TimestamptzOID,
|
||||
Codec: &pgtype.TimestamptzCodec{ScanLocation: time.UTC},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
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 their versions; a session
|
||||
// advisory lock makes overlapping runs wait for each other.
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) ([]int64, error) {
|
||||
locker, err := lock.NewPostgresSessionLocker()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("migration lock: %w", err)
|
||||
}
|
||||
db := stdlib.OpenDBFromPool(pool)
|
||||
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.FS, goose.WithSessionLocker(locker))
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("load migrations: %w", err)
|
||||
}
|
||||
defer provider.Close()
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user