From 004ca1aa80ef96c1dde081abaeec5f377ebc4e6e Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Thu, 9 Nov 2017 11:21:18 -0800 Subject: [PATCH] clientv3/balancer: only notify healthy addresses Signed-off-by: Gyu-Ho Lee --- clientv3/balancer.go | 10 +++------- clientv3/balancer_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/clientv3/balancer.go b/clientv3/balancer.go index 38d33b31a10c..dc6099c3e612 100644 --- a/clientv3/balancer.go +++ b/clientv3/balancer.go @@ -365,19 +365,15 @@ func (b *simpleBalancer) notifyAddrs(msg notifyMsg) { } } b.mu.RLock() - addrs := b.addrs pinAddr := b.pinAddr downc := b.downc b.mu.RUnlock() + addrs, hostPorts := b.liveAddrs() var waitDown bool if pinAddr != "" { - waitDown = true - for _, a := range addrs { - if a.Addr == pinAddr { - waitDown = false - } - } + _, ok := hostPorts[pinAddr] + waitDown = !ok } select { diff --git a/clientv3/balancer_test.go b/clientv3/balancer_test.go index 0ce4114eccff..0152585185c8 100644 --- a/clientv3/balancer_test.go +++ b/clientv3/balancer_test.go @@ -66,8 +66,8 @@ func TestBalancerGetUnblocking(t *testing.T) { } down1(errors.New("error")) - if addrs := <-sb.Notify(); len(addrs) != len(endpoints) { - t.Errorf("closing the only connection should triggered balancer to send the all endpoints via Notify chan so that we can establish a connection") + if addrs := <-sb.Notify(); len(addrs) != 2 { + t.Errorf("closing the only connection should triggered balancer to send the 2 endpoints via Notify chan so that we can establish a connection") } down2(errors.New("error")) _, _, err = sb.Get(context.Background(), unblockingOpts) @@ -121,8 +121,8 @@ func TestBalancerGetBlocking(t *testing.T) { } down1(errors.New("error")) - if addrs := <-sb.Notify(); len(addrs) != len(endpoints) { - t.Errorf("closing the only connection should triggered balancer to send the all endpoints via Notify chan so that we can establish a connection") + if addrs := <-sb.Notify(); len(addrs) != 2 { + t.Errorf("closing the only connection should triggered balancer to send the 2 endpoints via Notify chan so that we can establish a connection") } down2(errors.New("error")) ctx, cancel = context.WithTimeout(context.Background(), time.Millisecond*100)