Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow setting negative values to disable keepalive and write timeout #682

Merged
merged 1 commit into from
Nov 27, 2024
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
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