forked from mirror/redis
Rename mutex properties
This commit is contained in:
parent
f5091d4be5
commit
e428ae1457
24
cluster.go
24
cluster.go
|
@ -16,10 +16,10 @@ type ClusterClient struct {
|
||||||
|
|
||||||
addrs map[string]struct{}
|
addrs map[string]struct{}
|
||||||
slots [][]string
|
slots [][]string
|
||||||
sLock sync.RWMutex // protects slots & addrs cache
|
slotsMx sync.RWMutex // protects slots & addrs cache
|
||||||
|
|
||||||
conns map[string]*Client
|
conns map[string]*Client
|
||||||
cLock sync.Mutex // protects conns
|
connMx sync.Mutex // protects conns
|
||||||
|
|
||||||
opt *ClusterOptions
|
opt *ClusterOptions
|
||||||
|
|
||||||
|
@ -47,8 +47,8 @@ func NewClusterClient(opt *ClusterOptions) (*ClusterClient, error) {
|
||||||
|
|
||||||
// Close closes the cluster connection
|
// Close closes the cluster connection
|
||||||
func (c *ClusterClient) Close() error {
|
func (c *ClusterClient) Close() error {
|
||||||
c.sLock.Lock()
|
c.slotsMx.Lock()
|
||||||
defer c.sLock.Unlock()
|
defer c.slotsMx.Unlock()
|
||||||
|
|
||||||
return c.reset()
|
return c.reset()
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ func (c *ClusterClient) getMasterAddrBySlot(hashSlot int) string {
|
||||||
|
|
||||||
// Returns a node's client for a given address
|
// Returns a node's client for a given address
|
||||||
func (c *ClusterClient) getNodeClientByAddr(addr string) *Client {
|
func (c *ClusterClient) getNodeClientByAddr(addr string) *Client {
|
||||||
c.cLock.Lock()
|
c.connMx.Lock()
|
||||||
client, ok := c.conns[addr]
|
client, ok := c.conns[addr]
|
||||||
if !ok {
|
if !ok {
|
||||||
opt := c.opt.clientOptions()
|
opt := c.opt.clientOptions()
|
||||||
|
@ -73,7 +73,7 @@ func (c *ClusterClient) getNodeClientByAddr(addr string) *Client {
|
||||||
client = NewTCPClient(opt)
|
client = NewTCPClient(opt)
|
||||||
c.conns[addr] = client
|
c.conns[addr] = client
|
||||||
}
|
}
|
||||||
c.cLock.Unlock()
|
c.connMx.Unlock()
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,8 +85,8 @@ func (c *ClusterClient) process(cmd Cmder) {
|
||||||
|
|
||||||
hashSlot := hashSlot(cmd.clusterKey())
|
hashSlot := hashSlot(cmd.clusterKey())
|
||||||
|
|
||||||
c.sLock.RLock()
|
c.slotsMx.RLock()
|
||||||
defer c.sLock.RUnlock()
|
defer c.slotsMx.RUnlock()
|
||||||
|
|
||||||
tried := make(map[string]struct{}, len(c.addrs))
|
tried := make(map[string]struct{}, len(c.addrs))
|
||||||
addr := c.getMasterAddrBySlot(hashSlot)
|
addr := c.getMasterAddrBySlot(hashSlot)
|
||||||
|
@ -150,8 +150,8 @@ func (c *ClusterClient) reloadIfDue() (err error) {
|
||||||
|
|
||||||
var infos []ClusterSlotInfo
|
var infos []ClusterSlotInfo
|
||||||
|
|
||||||
c.sLock.Lock()
|
c.slotsMx.Lock()
|
||||||
defer c.sLock.Unlock()
|
defer c.slotsMx.Unlock()
|
||||||
|
|
||||||
// Try known addresses in random order (map interation order is random in Go)
|
// Try known addresses in random order (map interation order is random in Go)
|
||||||
// http://redis.io/topics/cluster-spec#clients-first-connection-and-handling-of-redirections
|
// http://redis.io/topics/cluster-spec#clients-first-connection-and-handling-of-redirections
|
||||||
|
@ -170,14 +170,14 @@ func (c *ClusterClient) reloadIfDue() (err error) {
|
||||||
|
|
||||||
// Closes all connections and flushes slots cache
|
// Closes all connections and flushes slots cache
|
||||||
func (c *ClusterClient) reset() (err error) {
|
func (c *ClusterClient) reset() (err error) {
|
||||||
c.cLock.Lock()
|
c.connMx.Lock()
|
||||||
for addr, client := range c.conns {
|
for addr, client := range c.conns {
|
||||||
if e := client.Close(); e != nil {
|
if e := client.Close(); e != nil {
|
||||||
err = e
|
err = e
|
||||||
}
|
}
|
||||||
delete(c.conns, addr)
|
delete(c.conns, addr)
|
||||||
}
|
}
|
||||||
c.cLock.Unlock()
|
c.connMx.Unlock()
|
||||||
c.slots = make([][]string, hashSlots)
|
c.slots = make([][]string, hashSlots)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue