Merge pull request #1414 from go-redis/fix/load-cmds-3-attempts

Try 3 random addrs to load commands
This commit is contained in:
Vladimir Mihailenco 2020-07-19 10:28:06 +03:00 committed by GitHub
commit 16e62e05a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 7 deletions

View File

@ -350,7 +350,7 @@ func (c *clusterNodes) Get(addr string) (*clusterNode, error) {
node, ok := c.nodes[addr] node, ok := c.nodes[addr]
if ok { if ok {
return node, err return node, nil
} }
node = newClusterNode(c.opt, addr) node = newClusterNode(c.opt, addr)
@ -358,7 +358,7 @@ func (c *clusterNodes) Get(addr string) (*clusterNode, error) {
c.addrs = appendIfNotExists(c.addrs, addr) c.addrs = appendIfNotExists(c.addrs, addr)
c.nodes[addr] = node c.nodes[addr] = node
return node, err return node, nil
} }
func (c *clusterNodes) get(addr string) (*clusterNode, error) { func (c *clusterNodes) get(addr string) (*clusterNode, error) {
@ -1542,22 +1542,33 @@ func (c *ClusterClient) retryBackoff(attempt int) time.Duration {
} }
func (c *ClusterClient) cmdsInfo() (map[string]*CommandInfo, error) { func (c *ClusterClient) cmdsInfo() (map[string]*CommandInfo, error) {
// Try 3 random nodes.
const nodeLimit = 3
addrs, err := c.nodes.Addrs() addrs, err := c.nodes.Addrs()
if err != nil { if err != nil {
return nil, err return nil, err
} }
var firstErr error var firstErr error
for _, addr := range addrs {
perm := rand.Perm(len(addrs))
if len(perm) > nodeLimit {
perm = perm[:nodeLimit]
}
for _, idx := range perm {
addr := addrs[idx]
node, err := c.nodes.Get(addr) node, err := c.nodes.Get(addr)
if err != nil { if err != nil {
return nil, err if firstErr == nil {
} firstErr = err
if node == nil { }
continue continue
} }
info, err := node.Client.Command(context.TODO()).Result() info, err := node.Client.Command(c.ctx).Result()
if err == nil { if err == nil {
return info, nil return info, nil
} }
@ -1565,6 +1576,10 @@ func (c *ClusterClient) cmdsInfo() (map[string]*CommandInfo, error) {
firstErr = err firstErr = err
} }
} }
if firstErr == nil {
panic("not reached")
}
return nil, firstErr return nil, firstErr
} }