Skip to content

Commit ff2678b

Browse files
committed
proxy: add grace to blocking read timeouts
1 parent 7f3f917 commit ff2678b

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

proxy/backend.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
defaultDialTimeout = 5 * time.Second
1515
defaultReadTimeout = 3 * time.Second
1616
defaultWriteTimeout = 3 * time.Second
17+
blockingReadGrace = 10 * time.Second
1718
respProtocolV2 = 2
1819
)
1920

@@ -91,7 +92,14 @@ func (b *RedisBackend) Do(ctx context.Context, args ...any) *redis.Cmd {
9192
// This is used for blocking commands whose wait time exceeds the backend's
9293
// default read timeout.
9394
func (b *RedisBackend) DoWithTimeout(ctx context.Context, timeout time.Duration, args ...any) *redis.Cmd {
94-
return b.client.WithTimeout(timeout).Do(ctx, args...)
95+
return b.client.WithTimeout(effectiveBlockingReadTimeout(timeout)).Do(ctx, args...)
96+
}
97+
98+
func effectiveBlockingReadTimeout(timeout time.Duration) time.Duration {
99+
if timeout == 0 {
100+
return 0
101+
}
102+
return timeout + blockingReadGrace
95103
}
96104

97105
func (b *RedisBackend) Pipeline(ctx context.Context, cmds [][]any) ([]*redis.Cmd, error) {

proxy/proxy_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,12 @@ func TestNewRedisBackend_UsesRESP2(t *testing.T) {
777777
assert.Equal(t, respProtocolV2, backend.client.Options().Protocol)
778778
}
779779

780+
func TestEffectiveBlockingReadTimeout(t *testing.T) {
781+
assert.Equal(t, time.Duration(0), effectiveBlockingReadTimeout(0))
782+
assert.Equal(t, 20*time.Second, effectiveBlockingReadTimeout(10*time.Second))
783+
assert.Equal(t, 11*time.Second, effectiveBlockingReadTimeout(time.Second))
784+
}
785+
780786
// ========== Pipeline error handling tests ==========
781787

782788
func TestPipeline_TransportError(t *testing.T) {

0 commit comments

Comments
 (0)