Учесть замечания ревью: читать timestamptz в UTC, встроить списки колонок, не требовать каноническую PHC-строку, уточнить замеры отказов

This commit is contained in:
2026-10-21 15:08:17 +03:00
parent 956b84f2d7
commit 7aa86d36ba
7 changed files with 37 additions and 23 deletions
+1 -3
View File
@@ -10,9 +10,7 @@ import (
const keyUserID = "messenger.user_id"
// SetUserID records the authenticated caller for the handlers behind the
// authentication middleware and for the access log. It lives here rather
// than in the auth package because the access log needs it and the business
// modules must not depend on auth.
// authentication middleware and for the access log.
func SetUserID(c *gin.Context, id uuid.UUID) {
c.Set(keyUserID, id)
}
+1 -1
View File
@@ -54,7 +54,7 @@ func Verify(encoded, plain string) (bool, error) {
var memory, time uint32
var parallelism uint8
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &time, &parallelism); err != nil ||
memory == 0 || time == 0 || parallelism == 0 || params(memory, time, parallelism) != parts[3] {
memory == 0 || time == 0 || parallelism == 0 {
return false, ErrMalformed
}
salt, err := b64.DecodeString(parts[4])
+16 -3
View File
@@ -14,7 +14,9 @@ import (
"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"
@@ -40,9 +42,10 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
}
cfg.MaxConns = maxConns
cfg.ConnConfig.ConnectTimeout = connectTimeout
// pgconn applies ConnectTimeout to dialling but resolves the host name
// before that with the caller's context. Docker DNS takes seconds to give
// up on a stopped container, so the lookup gets the same bound.
// 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()
@@ -50,6 +53,16 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
}
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 {
@@ -31,6 +31,16 @@ func TestMain(m *testing.M) {
os.Exit(code)
}
func TestPoolScansTimestamptzInUTC(t *testing.T) {
var got time.Time
if err := db.Pool.QueryRow(context.Background(), `SELECT now()`).Scan(&got); err != nil {
t.Fatal(err)
}
if got.Location() != time.UTC {
t.Fatalf("timestamptz scanned in %v, want UTC", got.Location())
}
}
func TestMigrateIsRepeatable(t *testing.T) {
ctx := context.Background()
applied, err := postgres.Migrate(ctx, db.Pool)