From 40bad36dfb23f1fe0dbda4e26435610b17a2e41e Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 5 May 2015 12:48:49 +0300 Subject: [PATCH] cluster: don't reload slots if address already changed. --- cluster.go | 16 ++++++++++------ cluster_pipeline.go | 8 +------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/cluster.go b/cluster.go index d09298ee..02a2ed5b 100644 --- a/cluster.go +++ b/cluster.go @@ -97,6 +97,14 @@ func (c *ClusterClient) slotAddrs(slot int) []string { 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. func (c *ClusterClient) randomClient() (client *Client, err error) { for i := 0; i < 10; i++ { @@ -118,11 +126,7 @@ func (c *ClusterClient) process(cmd Cmder) { slot := hashSlot(cmd.clusterKey()) - var addr string - if addrs := c.slotAddrs(slot); len(addrs) > 0 { - addr = addrs[0] // First address is master. - } - + addr := c.slotMasterAddr(slot) client, err := c.getClient(addr) if err != nil { cmd.setErr(err) @@ -163,7 +167,7 @@ func (c *ClusterClient) process(cmd Cmder) { var addr string moved, ask, addr = isMovedError(err) if moved || ask { - if moved { + if moved && c.slotMasterAddr(slot) != addr { c.lazyReloadSlots() } client, err = c.getClient(addr) diff --git a/cluster_pipeline.go b/cluster_pipeline.go index 2ddc064d..466be7d4 100644 --- a/cluster_pipeline.go +++ b/cluster_pipeline.go @@ -53,13 +53,7 @@ func (c *ClusterPipeline) Exec() (cmds []Cmder, retErr error) { cmdsMap := make(map[string][]Cmder) for _, cmd := range cmds { slot := hashSlot(cmd.clusterKey()) - addrs := c.cluster.slotAddrs(slot) - - var addr string - if len(addrs) > 0 { - addr = addrs[0] // First address is master. - } - + addr := c.cluster.slotMasterAddr(slot) cmdsMap[addr] = append(cmdsMap[addr], cmd) }