cluster: don't reload slots if address already changed.

This commit is contained in:
Vladimir Mihailenco 2015-05-05 12:48:49 +03:00
parent b4f481ef43
commit 40bad36dfb
2 changed files with 11 additions and 13 deletions

View File

@ -97,6 +97,14 @@ func (c *ClusterClient) slotAddrs(slot int) []string {
return addrs return addrs
} }
func (c *ClusterClient) slotMasterAddr(slot int) string {
addrs := c.slotAddrs(slot)
if len(addrs) > 0 {
return addrs[0]
}
return ""
}
// randomClient returns a Client for the first live node. // randomClient returns a Client for the first live node.
func (c *ClusterClient) randomClient() (client *Client, err error) { func (c *ClusterClient) randomClient() (client *Client, err error) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -118,11 +126,7 @@ func (c *ClusterClient) process(cmd Cmder) {
slot := hashSlot(cmd.clusterKey()) slot := hashSlot(cmd.clusterKey())
var addr string addr := c.slotMasterAddr(slot)
if addrs := c.slotAddrs(slot); len(addrs) > 0 {
addr = addrs[0] // First address is master.
}
client, err := c.getClient(addr) client, err := c.getClient(addr)
if err != nil { if err != nil {
cmd.setErr(err) cmd.setErr(err)
@ -163,7 +167,7 @@ func (c *ClusterClient) process(cmd Cmder) {
var addr string var addr string
moved, ask, addr = isMovedError(err) moved, ask, addr = isMovedError(err)
if moved || ask { if moved || ask {
if moved { if moved && c.slotMasterAddr(slot) != addr {
c.lazyReloadSlots() c.lazyReloadSlots()
} }
client, err = c.getClient(addr) client, err = c.getClient(addr)

View File

@ -53,13 +53,7 @@ func (c *ClusterPipeline) Exec() (cmds []Cmder, retErr error) {
cmdsMap := make(map[string][]Cmder) cmdsMap := make(map[string][]Cmder)
for _, cmd := range cmds { for _, cmd := range cmds {
slot := hashSlot(cmd.clusterKey()) slot := hashSlot(cmd.clusterKey())
addrs := c.cluster.slotAddrs(slot) addr := c.cluster.slotMasterAddr(slot)
var addr string
if len(addrs) > 0 {
addr = addrs[0] // First address is master.
}
cmdsMap[addr] = append(cmdsMap[addr], cmd) cmdsMap[addr] = append(cmdsMap[addr], cmd)
} }