feat(v2/redis): expose ContextTimeoutEnabled on Config

What does this MR do?

Adds ContextTimeoutEnabled to redis.Config and passes it into the go-redis options, so a consumer can bind the context it passes to a command to the socket read and write deadlines.

Why

A per-call context deadline around a go-redis command does not bound the socket. baseClient.context() decides what the connection sees (go-redis v9.22.0, redis.go:1465-1473):

func (c *baseClient) context(ctx context.Context) context.Context {
	if c.opt.ContextTimeoutEnabled {
		return ctx
	}
	return context.Background()
}

That value is what reaches cn.WithWriter and cn.WithReader. With the flag unset — the default — the caller's deadline never reaches the connection, so a command on a live connection that Redis accepts and never answers blocks for ReadTimeout on each attempt.

The deadline is not doing nothing. It still reaches the pool wait (waitTurn selects on ctx.Done()) and the retry back-off (internal.Sleep), so it collapses up to four attempts into roughly one. It cannot bound that one attempt.

Config had no field for the flag and the go-redis options struct is built inside NewWithConfig, so nothing in a consumer's override surface could reach it, and *Client exposes no post-construction hook either. Call sites downstream document their deadline as a bound on the round trip; today that claim is wrong as written.

The behaviour we want already exists upstream behind this one flag. Conn.deadline takes the earlier of the socket timeout and the context deadline (internal/pool/conn.go:1207-1235), so with the flag on each call site's existing per-call deadline becomes its own effective read deadline, and ReadTimeout stays the shared ceiling for calls that set none.

Implementation notes

No behaviour change for existing consumers. go-redis already defaults ContextTimeoutEnabled to false, so a field left at its zero value changes nothing for anyone; every existing client keeps today's behaviour until its owner sets the field. This MR exposes the flag, it does not default it on.

*bool, not bool, following the DisableClientCommands precedent. The WithClientConfig override block is non-zero-wins, so a plain bool could only ever turn the flag on and never off.

No infrastructure-contract counterpart. The Redis message in protos/gen/go/gitlab/infrastructure has no matching field, and none is proposed here: this is per-purpose tuning rather than a property of the Redis deployment. WithClientConfig is the only source, and the override arm is wired up — without it the field would be silently dropped for any consumer that reaches LabKit through New rather than NewWithConfig.

Documented caveat. With the flag on, a request-scoped deadline ends any command it is nearer than ReadTimeout, and blocking commands are bounded by it too: go-redis exempts BLPop, XRead and the like from ReadTimeout, but not from the context deadline. A blocking command with an infinite block resolves to a socket timeout of 0, and Conn.deadline then returns the context deadline outright. Both the field doc and the package doc say so.

Tests

  • TestNewWithConfig_ContextTimeoutEnabled asserts the flag reaches the constructed go-redis client for single-node, Cluster and Sentinel separately. go-redis carries it on a distinct field per topology option struct, so a pass-through that only reached UniversalOptions.Simple() would leave Cluster and Sentinel deployments silently unbound.

  • TestContextTimeoutEnabled_BoundsSocketRead asserts the bound itself against a TCP server that completes the handshake and then accepts a GET it never answers. Unset, the command runs to ReadTimeout (~1s); set, it returns at its own deadline (~200ms).

    A healthy Redis cannot tell the fix from its absence, and a client stub that honours the context proves less than nothing: it passes while the real client does not. The test was checked by mutation — hardcoding the pass-through to false fails it with "1.000275625s" is not less than "1s".

  • The WithClientConfig override test covers the new field.

Merge request reports

Loading
Loading