From 62fc2c821b7b9d757a5dee74a4c17e0010c8301e Mon Sep 17 00:00:00 2001 From: Anatoly Rugalev Date: Sat, 17 Jul 2021 18:20:48 +0200 Subject: [PATCH] Check Failing() before serving random node (#1825) * Check Failing() before serving random node * Revert condition * Addressed review comments * Fallback to random failing node --- cluster.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cluster.go b/cluster.go index 9690f88a..8d93b369 100644 --- a/cluster.go +++ b/cluster.go @@ -595,8 +595,16 @@ func (c *clusterState) slotRandomNode(slot int) (*clusterNode, error) { if len(nodes) == 0 { return c.nodes.Random() } - n := rand.Intn(len(nodes)) - return nodes[n], nil + if len(nodes) == 1 { + return nodes[0], nil + } + randomNodes := rand.Perm(len(nodes)) + for _, idx := range randomNodes { + if node := nodes[idx]; !node.Failing() { + return node, nil + } + } + return nodes[randomNodes[0]], nil } func (c *clusterState) slotNodes(slot int) []*clusterNode {