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

fix: set timeout on refreshing cluster slots to avoid being stuck #615

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
14 changes: 8 additions & 6 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ func (s clusterslots) parse(tls bool) map[string]group {
return parseShards(s.reply.val, s.addr, tls)
}

func getClusterSlots(c conn) clusterslots {
func getClusterSlots(c conn, timeout time.Duration) clusterslots {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
v := c.Version()
if v < 7 {
return clusterslots{reply: c.Do(context.Background(), cmds.SlotCmd), addr: c.Addr(), ver: v}
return clusterslots{reply: c.Do(ctx, cmds.SlotCmd), addr: c.Addr(), ver: v}
}
return clusterslots{reply: c.Do(context.Background(), cmds.ShardsCmd), addr: c.Addr(), ver: v}
return clusterslots{reply: c.Do(ctx, cmds.ShardsCmd), addr: c.Addr(), ver: v}
}

func (c *clusterClient) _refresh() (err error) {
Expand All @@ -154,9 +156,9 @@ func (c *clusterClient) _refresh() (err error) {
for i := 0; i < cap(results); i++ {
if i&3 == 0 { // batch CLUSTER SLOTS/CLUSTER SHARDS for every 4 connections
for j := i; j < i+4 && j < len(pending); j++ {
go func(c conn) {
results <- getClusterSlots(c)
}(pending[j])
go func(c conn, timeout time.Duration) {
results <- getClusterSlots(c, timeout)
}(pending[j], c.opt.ConnWriteTimeout)
}
}
result = <-results
Expand Down
Loading