mirror of https://github.com/go-redis/redis.git
Merge pull request #375 from go-redis/fix/cluster-without-nodes
Don't panic when cluster does not have valid nodes.
This commit is contained in:
commit
bb84d84aea
21
cluster.go
21
cluster.go
|
@ -162,17 +162,18 @@ func (c *ClusterClient) newNode(addr string) *clusterNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClusterClient) slotNodes(slot int) []*clusterNode {
|
func (c *ClusterClient) slotNodes(slot int) (nodes []*clusterNode) {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
nodes := c.slots[slot]
|
if slot < len(c.slots) {
|
||||||
|
nodes = c.slots[slot]
|
||||||
|
}
|
||||||
c.mu.RUnlock()
|
c.mu.RUnlock()
|
||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
// randomNode returns random live node.
|
// randomNode returns random live node.
|
||||||
func (c *ClusterClient) randomNode() (*clusterNode, error) {
|
func (c *ClusterClient) randomNode() (*clusterNode, error) {
|
||||||
var node *clusterNode
|
var nodeErr error
|
||||||
var err error
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
closed := c.closed
|
closed := c.closed
|
||||||
|
@ -184,16 +185,18 @@ func (c *ClusterClient) randomNode() (*clusterNode, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
n := rand.Intn(len(addrs))
|
n := rand.Intn(len(addrs))
|
||||||
node, err = c.nodeByAddr(addrs[n])
|
|
||||||
|
node, err := c.nodeByAddr(addrs[n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if node.Client.ClusterInfo().Err() == nil {
|
nodeErr = node.Client.ClusterInfo().Err()
|
||||||
break
|
if nodeErr == nil {
|
||||||
}
|
|
||||||
}
|
|
||||||
return node, nil
|
return node, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nodeErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClusterClient) slotMasterNode(slot int) (*clusterNode, error) {
|
func (c *ClusterClient) slotMasterNode(slot int) (*clusterNode, error) {
|
||||||
|
|
|
@ -515,6 +515,19 @@ var _ = Describe("ClusterClient", func() {
|
||||||
|
|
||||||
describeClusterClient()
|
describeClusterClient()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("ClusterClient without valid nodes", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
client = redis.NewClusterClient(&redis.ClusterOptions{
|
||||||
|
Addrs: []string{redisAddr},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
It("returns an error", func() {
|
||||||
|
err := client.Ping().Err()
|
||||||
|
Expect(err).To(MatchError("ERR This instance has cluster support disabled"))
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue