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

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)