From 2e5bd7a4116e88ed0d7c87108db102b404b3faaa Mon Sep 17 00:00:00 2001
From: Vladimir Mihailenco <vladimir.webdev@gmail.com>
Date: Tue, 8 Jan 2019 16:37:22 +0200
Subject: [PATCH] Reload cluster state almost on any error

---
 cluster.go | 48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/cluster.go b/cluster.go
index f283722c..47b79244 100644
--- a/cluster.go
+++ b/cluster.go
@@ -844,15 +844,12 @@ func (c *ClusterClient) Watch(fn func(*Tx) error, keys ...string) error {
 		if err == nil {
 			break
 		}
-
-		if internal.IsRetryableError(err, true) {
+		if err != Nil {
 			c.state.LazyReload()
-			continue
 		}
 
 		moved, ask, addr := internal.IsMovedError(err)
 		if moved || ask {
-			c.state.LazyReload()
 			node, err = c.nodes.GetOrCreate(addr)
 			if err != nil {
 				return err
@@ -868,6 +865,10 @@ func (c *ClusterClient) Watch(fn func(*Tx) error, keys ...string) error {
 			continue
 		}
 
+		if internal.IsRetryableError(err, true) {
+			continue
+		}
+
 		return err
 	}
 
@@ -932,6 +933,9 @@ func (c *ClusterClient) defaultProcess(cmd Cmder) error {
 		if err == nil {
 			break
 		}
+		if err != Nil {
+			c.state.LazyReload()
+		}
 
 		// If slave is loading - pick another node.
 		if c.opt.ReadOnly && internal.IsLoadingError(err) {
@@ -940,9 +944,23 @@ func (c *ClusterClient) defaultProcess(cmd Cmder) error {
 			continue
 		}
 
-		if internal.IsRetryableError(err, true) {
-			c.state.LazyReload()
+		var moved bool
+		var addr string
+		moved, ask, addr = internal.IsMovedError(err)
+		if moved || ask {
+			node, err = c.nodes.GetOrCreate(addr)
+			if err != nil {
+				break
+			}
+			continue
+		}
 
+		if err == pool.ErrClosed {
+			node = nil
+			continue
+		}
+
+		if internal.IsRetryableError(err, true) {
 			// First retry the same node.
 			if attempt == 0 {
 				continue
@@ -956,24 +974,6 @@ func (c *ClusterClient) defaultProcess(cmd Cmder) error {
 			continue
 		}
 
-		var moved bool
-		var addr string
-		moved, ask, addr = internal.IsMovedError(err)
-		if moved || ask {
-			c.state.LazyReload()
-
-			node, err = c.nodes.GetOrCreate(addr)
-			if err != nil {
-				break
-			}
-			continue
-		}
-
-		if err == pool.ErrClosed {
-			node = nil
-			continue
-		}
-
 		break
 	}