Ограничить дозвон и поиск имени зависимостей одной секундой

This commit is contained in:
2026-10-06 15:54:26 +03:00
parent f9850a36c4
commit bea31f3f02
4 changed files with 33 additions and 10 deletions
+9 -1
View File
@@ -26,7 +26,7 @@ import (
const (
maxConns = 10
connectTimeout = 3 * time.Second
connectTimeout = time.Second
statementTimeout = 5 * time.Second
idleInTxTimeout = 10 * time.Second
)
@@ -40,6 +40,14 @@ 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.
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)
@@ -8,6 +8,7 @@ import (
"net"
"os"
"testing"
"time"
"github.com/jackc/pgx/v5/pgconn"
@@ -61,6 +62,9 @@ func TestPoolAppliesLimitsAndTimeouts(t *testing.T) {
if got := db.Pool.Config().MaxConns; got != 10 {
t.Fatalf("MaxConns = %d, want 10", got)
}
if got := db.Pool.Config().ConnConfig.ConnectTimeout; got != time.Second {
t.Fatalf("ConnectTimeout = %v, want 1s", got)
}
ctx := context.Background()
for setting, want := range map[string]string{
"statement_timeout": "5s",