From 84ae9866599493e939021f55d40ded0e8f855cc0 Mon Sep 17 00:00:00 2001 From: yyoshiki41 Date: Thu, 6 Oct 2016 05:20:05 +0900 Subject: [PATCH] Remove Addr field from clusterNode --- cluster.go | 8 +++----- cluster_client_test.go | 34 +++++++++++++++++----------------- redis.go | 6 +++++- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/cluster.go b/cluster.go index ad56af70..9baad73a 100644 --- a/cluster.go +++ b/cluster.go @@ -13,7 +13,6 @@ import ( ) type clusterNode struct { - Addr string Client *Client Latency time.Duration } @@ -152,7 +151,7 @@ func (c *ClusterClient) nodeByAddr(addr string) (*clusterNode, error) { if !ok { node = c.newNode(addr) c.nodes[addr] = node - c.addrs = append(c.addrs, node.Addr) + c.addrs = append(c.addrs, addr) } return node, nil @@ -162,7 +161,6 @@ func (c *ClusterClient) newNode(addr string) *clusterNode { opt := c.opt.clientOptions() opt.Addr = addr return &clusterNode{ - Addr: addr, Client: NewClient(opt), } } @@ -313,7 +311,7 @@ func (c *ClusterClient) Process(cmd Cmder) error { moved, ask, addr = errors.IsMoved(err) if moved || ask { master, _ := c.slotMasterNode(slot) - if moved && (master == nil || master.Addr != addr) { + if moved && (master == nil || master.Client.getAddr() != addr) { c.lazyReloadSlots() } @@ -425,7 +423,7 @@ func (c *ClusterClient) reloadSlots() { slots, err := node.Client.ClusterSlots().Result() if err != nil { - internal.Logf("ClusterSlots on addr=%q failed: %s", node.Addr, err) + internal.Logf("ClusterSlots on addr=%q failed: %s", node.Client.getAddr(), err) return } diff --git a/cluster_client_test.go b/cluster_client_test.go index 8277abdf..0a30d5da 100644 --- a/cluster_client_test.go +++ b/cluster_client_test.go @@ -8,7 +8,7 @@ import ( func (c *ClusterClient) SlotAddrs(slot int) []string { var addrs []string for _, n := range c.slotNodes(slot) { - addrs = append(addrs, n.Addr) + addrs = append(addrs, n.Client.getAddr()) } return addrs } @@ -51,22 +51,22 @@ var _ = Describe("ClusterClient", func() { It("should update slots cache", func() { populate() - Expect(subject.slots[0][0].Addr).To(Equal("127.0.0.1:7000")) - Expect(subject.slots[0][1].Addr).To(Equal("127.0.0.1:7004")) - Expect(subject.slots[4095][0].Addr).To(Equal("127.0.0.1:7000")) - Expect(subject.slots[4095][1].Addr).To(Equal("127.0.0.1:7004")) - Expect(subject.slots[4096][0].Addr).To(Equal("127.0.0.1:7001")) - Expect(subject.slots[4096][1].Addr).To(Equal("127.0.0.1:7005")) - Expect(subject.slots[8191][0].Addr).To(Equal("127.0.0.1:7001")) - Expect(subject.slots[8191][1].Addr).To(Equal("127.0.0.1:7005")) - Expect(subject.slots[8192][0].Addr).To(Equal("127.0.0.1:7002")) - Expect(subject.slots[8192][1].Addr).To(Equal("127.0.0.1:7006")) - Expect(subject.slots[12287][0].Addr).To(Equal("127.0.0.1:7002")) - Expect(subject.slots[12287][1].Addr).To(Equal("127.0.0.1:7006")) - Expect(subject.slots[12288][0].Addr).To(Equal("127.0.0.1:7003")) - Expect(subject.slots[12288][1].Addr).To(Equal("127.0.0.1:7007")) - Expect(subject.slots[16383][0].Addr).To(Equal("127.0.0.1:7003")) - Expect(subject.slots[16383][1].Addr).To(Equal("127.0.0.1:7007")) + Expect(subject.slots[0][0].Client.getAddr()).To(Equal("127.0.0.1:7000")) + Expect(subject.slots[0][1].Client.getAddr()).To(Equal("127.0.0.1:7004")) + Expect(subject.slots[4095][0].Client.getAddr()).To(Equal("127.0.0.1:7000")) + Expect(subject.slots[4095][1].Client.getAddr()).To(Equal("127.0.0.1:7004")) + Expect(subject.slots[4096][0].Client.getAddr()).To(Equal("127.0.0.1:7001")) + Expect(subject.slots[4096][1].Client.getAddr()).To(Equal("127.0.0.1:7005")) + Expect(subject.slots[8191][0].Client.getAddr()).To(Equal("127.0.0.1:7001")) + Expect(subject.slots[8191][1].Client.getAddr()).To(Equal("127.0.0.1:7005")) + Expect(subject.slots[8192][0].Client.getAddr()).To(Equal("127.0.0.1:7002")) + Expect(subject.slots[8192][1].Client.getAddr()).To(Equal("127.0.0.1:7006")) + Expect(subject.slots[12287][0].Client.getAddr()).To(Equal("127.0.0.1:7002")) + Expect(subject.slots[12287][1].Client.getAddr()).To(Equal("127.0.0.1:7006")) + Expect(subject.slots[12288][0].Client.getAddr()).To(Equal("127.0.0.1:7003")) + Expect(subject.slots[12288][1].Client.getAddr()).To(Equal("127.0.0.1:7007")) + Expect(subject.slots[16383][0].Client.getAddr()).To(Equal("127.0.0.1:7003")) + Expect(subject.slots[16383][1].Client.getAddr()).To(Equal("127.0.0.1:7007")) Expect(subject.addrs).To(Equal([]string{ "127.0.0.1:6379", "127.0.0.1:7003", diff --git a/redis.go b/redis.go index 4b535ff4..60df328e 100644 --- a/redis.go +++ b/redis.go @@ -24,7 +24,7 @@ type baseClient struct { } func (c *baseClient) String() string { - return fmt.Sprintf("Redis<%s db:%d>", c.opt.Addr, c.opt.DB) + return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB) } func (c *baseClient) conn() (*pool.Conn, bool, error) { @@ -140,6 +140,10 @@ func (c *baseClient) Close() error { return retErr } +func (c *baseClient) getAddr() string { + return c.opt.Addr +} + //------------------------------------------------------------------------------ // Client is a Redis client representing a pool of zero or more