Skip to content

Commit

Permalink
feat: allow setting negative values to disable keepalive and write ti…
Browse files Browse the repository at this point in the history
…meout (#682)
  • Loading branch information
rueian authored Nov 27, 2024
1 parent 3ee5776 commit 06ed4fa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
10 changes: 8 additions & 2 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,14 @@ func (s clusterslots) parse(tls bool) map[string]group {
}

func getClusterSlots(c conn, timeout time.Duration) clusterslots {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
var ctx context.Context
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
defer cancel()
} else {
ctx = context.Background()
}
v := c.Version()
if v < 8 {
return clusterslots{reply: c.Do(ctx, cmds.SlotCmd), addr: c.Addr(), ver: v}
Expand Down
37 changes: 37 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,3 +1015,40 @@ func TestKvrocksSingleClientIntegration(t *testing.T) {

client.Close()
}

func TestNegativeConnWriteTimeout(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
client, err := NewClient(ClientOption{
InitAddress: []string{"127.0.0.1:6379"},
ConnWriteTimeout: -1,
})
if err != nil {
t.Fatal(err)
}
client.Close()
}

func TestNegativeKeepalive(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
client, err := NewClient(ClientOption{
InitAddress: []string{"127.0.0.1:6379"},
Dialer: net.Dialer{KeepAlive: -1},
})
if err != nil {
t.Fatal(err)
}
client.Close()
}

func TestNegativeConnWriteTimeoutKeepalive(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
client, err := NewClient(ClientOption{
InitAddress: []string{"127.0.0.1:6379"},
Dialer: net.Dialer{KeepAlive: -1},
ConnWriteTimeout: -1,
})
if err != nil {
t.Fatal(err)
}
client.Close()
}
2 changes: 1 addition & 1 deletion rueidis.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func NewClient(option ClientOption) (client Client, err error) {
option.Dialer.KeepAlive = DefaultTCPKeepAlive
}
if option.ConnWriteTimeout == 0 {
option.ConnWriteTimeout = option.Dialer.KeepAlive * 10
option.ConnWriteTimeout = max(DefaultTCPKeepAlive, option.Dialer.KeepAlive) * 10
}
if option.BlockingPipeline == 0 {
option.BlockingPipeline = DefaultBlockingPipeline
Expand Down

0 comments on commit 06ed4fa

Please sign in to comment.