Skip to content

Commit

Permalink
fix minlatency zero value
Browse files Browse the repository at this point in the history
  • Loading branch information
srikar-jilugu committed Jul 8, 2024
1 parent 2b11108 commit 34c1db1
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions osscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ func (c *clusterState) slotClosestNode(slot int) (*clusterNode, error) {
minLatency time.Duration
)

// setting the max possible duration as zerovalue for minlatency
minLatency = time.Duration(math.MaxInt64)

for _, n := range nodes {
if closestNode == nil || n.Latency() < minLatency {
closestNode = n
Expand All @@ -761,15 +764,14 @@ func (c *clusterState) slotClosestNode(slot int) (*clusterNode, error) {
}

// if all nodes are failing, we will pick the temporarily failing node with lowest latency
if minLatency < maximumNodeLatency {
internal.Logger.Printf(context.TODO(), "redis: all nodes are failing, picking the temporarily failing node with lowest latency")
if minLatency < maximumNodeLatency && closestNode != nil {
internal.Logger.Printf(context.TODO(), "redis: all nodes are marked as failed, picking the temporarily failing node with lowest latency")
return closestNode, nil
}

// If all nodes are having the maximum latency(all pings are failing) - return a random node within the nodes corresponding to the slot
randomNodes := rand.Perm(len(nodes))
internal.Logger.Printf(context.TODO(), "redis: pings to all nodes are failing, picking a random node within the slot")
return nodes[randomNodes[0]], nil
// If all nodes are having the maximum latency(all pings are failing) - return a random node across the cluster
internal.Logger.Printf(context.TODO(), "redis: pings to all nodes are failing, picking a random node across the cluster")
return c.nodes.Random()
}

func (c *clusterState) slotRandomNode(slot int) (*clusterNode, error) {
Expand Down

0 comments on commit 34c1db1

Please sign in to comment.