From bea31f3f0205dd588f2704c286b6469a8f54b878 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:54:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D0=B4=D0=BE=D0=B7=D0=B2=D0=BE=D0=BD=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B5=D0=B9=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=D1=81=D0=B5=D0=BA=D1=83=D0=BD=D0=B4=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/platform/postgres/postgres.go | 10 +++++++++- internal/platform/postgres/postgres_test.go | 4 ++++ internal/platform/valkey/valkey.go | 19 ++++++++++++------- internal/platform/valkey/valkey_test.go | 10 ++++++++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/internal/platform/postgres/postgres.go b/internal/platform/postgres/postgres.go index 41c61100..45f4eda9 100644 --- a/internal/platform/postgres/postgres.go +++ b/internal/platform/postgres/postgres.go @@ -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) diff --git a/internal/platform/postgres/postgres_test.go b/internal/platform/postgres/postgres_test.go index 4546b370..b3b3a917 100644 --- a/internal/platform/postgres/postgres_test.go +++ b/internal/platform/postgres/postgres_test.go @@ -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", diff --git a/internal/platform/valkey/valkey.go b/internal/platform/valkey/valkey.go index 15a4c0af..70c5ba5d 100644 --- a/internal/platform/valkey/valkey.go +++ b/internal/platform/valkey/valkey.go @@ -1,9 +1,12 @@ // Package valkey connects to the Valkey session store. // // Timeouts are constants: every authenticated request reads a session, so -// they bound how long a request can wait when the store is slow. Retries are -// disabled for the same reason: a request that cannot reach the store fails -// fast with 503 instead of waiting through a backoff. +// they bound how long a request can wait when the store is slow. Command +// retries are disabled and a connection is dialled once for the same reason: +// a request that cannot reach the store fails fast with 503 instead of +// waiting through a backoff. On the compose stand Docker DNS takes seconds +// to give up on a stopped container, so one dial attempt is already bounded +// only by dialTimeout. package valkey import ( @@ -16,10 +19,11 @@ import ( ) const ( - dialTimeout = 2 * time.Second - ioTimeout = time.Second - poolSize = 10 - noRetries = -1 + dialTimeout = time.Second + ioTimeout = time.Second + poolSize = 10 + noRetries = -1 + singleAttempt = 1 ) // NewClient connects to url (redis:// scheme) and pings the server. Errors @@ -34,6 +38,7 @@ func NewClient(ctx context.Context, url string) (*redis.Client, error) { opts.WriteTimeout = ioTimeout opts.PoolSize = poolSize opts.MaxRetries = noRetries + opts.DialerRetries = singleAttempt client := redis.NewClient(opts) if err := client.Ping(ctx).Err(); err != nil { diff --git a/internal/platform/valkey/valkey_test.go b/internal/platform/valkey/valkey_test.go index af13203f..efb43ebf 100644 --- a/internal/platform/valkey/valkey_test.go +++ b/internal/platform/valkey/valkey_test.go @@ -24,8 +24,14 @@ func TestNewClientPingsAndBoundsWaiting(t *testing.T) { } defer client.Close() opts := client.Options() - if opts.DialTimeout != 2*time.Second || opts.ReadTimeout != time.Second || opts.WriteTimeout != time.Second { - t.Fatalf("timeouts = %v/%v/%v, want 2s/1s/1s", opts.DialTimeout, opts.ReadTimeout, opts.WriteTimeout) + if opts.DialTimeout != time.Second || opts.ReadTimeout != time.Second || opts.WriteTimeout != time.Second { + t.Fatalf("timeouts = %v/%v/%v, want 1s each", opts.DialTimeout, opts.ReadTimeout, opts.WriteTimeout) + } + // go-redis dials up to five times by default; with Docker DNS taking + // seconds to give up on a stopped container that alone would exceed the + // request deadline. + if opts.DialerRetries != 1 { + t.Fatalf("DialerRetries = %d, want a single dial attempt", opts.DialerRetries) } // go-redis stores the requested -1 ("no retries") as 0 after init; its // default would be 3.