diff --git a/cluster.go b/cluster.go index 03c186cf..3f140a6d 100644 --- a/cluster.go +++ b/cluster.go @@ -582,13 +582,6 @@ func (c *clusterState) slotNodes(slot int) []*clusterNode { return nil } -func (c *clusterState) IsConsistent() bool { - if c.nodes.opt.ClusterSlots != nil { - return true - } - return len(c.Masters) <= len(c.Slaves) -} - //------------------------------------------------------------------------------ type clusterStateHolder struct { @@ -613,9 +606,6 @@ func (c *clusterStateHolder) Reload() (*clusterState, error) { if err != nil { return nil, err } - if !state.IsConsistent() { - time.AfterFunc(time.Second, c.LazyReload) - } return state, nil } @@ -640,16 +630,11 @@ func (c *clusterStateHolder) LazyReload() { go func() { defer atomic.StoreUint32(&c.reloading, 0) - for { - state, err := c.reload() - if err != nil { - return - } - time.Sleep(100 * time.Millisecond) - if state.IsConsistent() { - return - } + _, err := c.reload() + if err != nil { + return } + time.Sleep(100 * time.Millisecond) }() } diff --git a/cluster_test.go b/cluster_test.go index b8003baa..cdbb7338 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -49,8 +49,6 @@ func (s *clusterScenario) addrs() []string { } func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.ClusterClient { - var errBadState = fmt.Errorf("cluster state is not consistent") - opt.Addrs = s.addrs() client := redis.NewClusterClient(opt) @@ -65,27 +63,7 @@ func (s *clusterScenario) clusterClient(opt *redis.ClusterOptions) *redis.Cluste } if !state.IsConsistent() { - return errBadState - } - - if len(state.Masters) < 3 { - return errBadState - } - for _, master := range state.Masters { - s := master.Client.Info("replication").Val() - if !strings.Contains(s, "role:master") { - return errBadState - } - } - - if len(state.Slaves) < 3 { - return errBadState - } - for _, slave := range state.Slaves { - s := slave.Client.Info("replication").Val() - if !strings.Contains(s, "role:slave") { - return errBadState - } + return fmt.Errorf("cluster state is not consistent") } return nil diff --git a/export_test.go b/export_test.go index fab91e2a..4dc0c628 100644 --- a/export_test.go +++ b/export_test.go @@ -3,6 +3,7 @@ package redis import ( "fmt" "net" + "strings" "github.com/go-redis/redis/internal/hashtag" "github.com/go-redis/redis/internal/pool" @@ -55,3 +56,27 @@ func (c *ClusterClient) SwapNodes(key string) error { nodes[0], nodes[1] = nodes[1], nodes[0] return nil } + +func (state *clusterState) IsConsistent() bool { + if len(state.Masters) < 3 { + return false + } + for _, master := range state.Masters { + s := master.Client.Info("replication").Val() + if !strings.Contains(s, "role:master") { + return false + } + } + + if len(state.Slaves) < 3 { + return false + } + for _, slave := range state.Slaves { + s := slave.Client.Info("replication").Val() + if !strings.Contains(s, "role:slave") { + return false + } + } + + return true +}