Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions driver/redis/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ type ClusterConfig struct {

PoolSize int
MaxIdlePerWorker int
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
// DialTimeout bounds each new node connection's TCP connect and, separately,
// its handshake; default 5s. See [Config].DialTimeout.
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
// MaxRedirects is the maximum number of MOVED/ASK redirects before an
// error is returned. Default: 3.
MaxRedirects int
Expand Down
47 changes: 46 additions & 1 deletion driver/redis/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,54 @@ func (c *redisConn) releaseHandshakeReq(req *redisRequest) {
c.releaseResult(req)
}

// testHookDefaultHandshakeTimeout, when non-nil, replaces defaultDialTimeout as
// the handshake bound of a conn whose Config.DialTimeout is unset. It is nil in
// production; a test sets it to see that the default is applied without waiting
// 5 s, and resets it to nil when it is done.
var testHookDefaultHandshakeTimeout atomic.Pointer[time.Duration]

// handshake runs HELLO (with optional fallback to AUTH+SELECT) to negotiate
// protocol version and authentication.
// protocol version and authentication. The whole exchange is bounded by
// Config.DialTimeout (default 5 s), on top of whatever deadline ctx carries: the
// dialer's timeout covers only the TCP connect (connect and handshake are two
// budgets, each DialTimeout), and a node that accepts and
// never answers HELLO would otherwise hold the dial until ctx ends, which is
// never under context.Background (celeris#1205). The three dial paths
// (dialRedisConn on unix and on the net.Conn loop, dialDirectRedisConn) all end
// here, so the pool's command and pub/sub dials are bounded alike.
func (c *redisConn) handshake(ctx context.Context) error {
timeout := c.cfg.DialTimeout
if timeout <= 0 {
timeout = defaultDialTimeout
if h := testHookDefaultHandshakeTimeout.Load(); h != nil {
timeout = *h
}
}
// Is the caller's own deadline the earlier one? Then it ends the handshake
// and is reported as the caller's.
callerFirst := false
if dl, ok := ctx.Deadline(); ok && !dl.After(time.Now().Add(timeout)) {
callerFirst = true
}
hctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err := c.runHandshake(hctx)
if err != nil && !callerFirst && ctx.Err() == nil {
// The direct path reports the socket's deadline (it can fire a hair
// before hctx's timer), the others the context's.
var rerr *RedisError
if (hctx.Err() != nil || errors.Is(err, os.ErrDeadlineExceeded)) && !errors.As(err, &rerr) {
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("celeris-redis: handshake timed out after %v: %w", timeout, err)
}
return fmt.Errorf("celeris-redis: handshake timed out after %v: %w: %w", timeout, context.DeadlineExceeded, err)
}
}
return err
}

// runHandshake is handshake without the bound.
func (c *redisConn) runHandshake(ctx context.Context) error {
if c.cfg.ForceRESP2 {
return c.legacyAuth(ctx)
}
Expand Down
Loading
Loading