From 6baf00e56b287527d75825165a336a0c79299935 Mon Sep 17 00:00:00 2001 From: WANG QIANG Date: Tue, 8 Jan 2019 15:25:41 +0800 Subject: [PATCH] Fix race condition when creating the cluster node --- cluster.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/cluster.go b/cluster.go index da5508a..5c1e093 100644 --- a/cluster.go +++ b/cluster.go @@ -17,7 +17,6 @@ import ( "github.com/go-redis/redis/internal/hashtag" "github.com/go-redis/redis/internal/pool" "github.com/go-redis/redis/internal/proto" - "github.com/go-redis/redis/internal/singleflight" ) var errClusterNoNodes = fmt.Errorf("redis: cluster has no nodes") @@ -243,8 +242,6 @@ type clusterNodes struct { clusterAddrs []string closed bool - nodeCreateGroup singleflight.Group - _generation uint32 // atomic } @@ -347,11 +344,6 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) { return node, nil } - v, err := c.nodeCreateGroup.Do(addr, func() (interface{}, error) { - node := newClusterNode(c.opt, addr) - return node, nil - }) - c.mu.Lock() defer c.mu.Unlock() @@ -361,10 +353,10 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) { node, ok := c.allNodes[addr] if ok { - _ = v.(*clusterNode).Close() return node, err } - node = v.(*clusterNode) + + node = newClusterNode(c.opt, addr) c.allAddrs = appendIfNotExists(c.allAddrs, addr) if err == nil {