From 9ca7444e5e7de023b8e9acd3d54429b472cb9f49 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Wed, 3 Apr 2019 17:46:57 -0700 Subject: [PATCH 1/2] addrConn: not reset backoff if update address in TransientFailure --- clientconn.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/clientconn.go b/clientconn.go index 8255caef929b..b5e69c75c54e 100644 --- a/clientconn.go +++ b/clientconn.go @@ -758,7 +758,6 @@ func (ac *addrConn) connect() error { ac.mu.Unlock() return nil } - ac.updateConnectivityState(connectivity.Connecting) ac.mu.Unlock() // Start a goroutine connecting to the server asynchronously. @@ -768,7 +767,16 @@ func (ac *addrConn) connect() error { // tryUpdateAddrs tries to update ac.addrs with the new addresses list. // -// It checks whether current connected address of ac is in the new addrs list. +// If ac is Connecting, it returnes false. The caller should tear down the ac +// and create a new one. Note that the backoff will be reset when this happens. +// +// If ac is TransientFailure, it updates ac.addrs and returns true. The updated +// addresses will be picked up by retry in the next iteration after backoff. +// +// If ac is Shutdown or Idle, it updates ac.addrs and returns true. +// +// If ac is Ready, it checks whether current connected address of ac is in the +// new addrs list. // - If true, it updates ac.addrs and returns true. The ac will keep using // the existing connection. // - If false, it does nothing and returns false. @@ -776,17 +784,18 @@ func (ac *addrConn) tryUpdateAddrs(addrs []resolver.Address) bool { ac.mu.Lock() defer ac.mu.Unlock() grpclog.Infof("addrConn: tryUpdateAddrs curAddr: %v, addrs: %v", ac.curAddr, addrs) - if ac.state == connectivity.Shutdown { + if ac.state == connectivity.Shutdown || + ac.state == connectivity.TransientFailure || + ac.state == connectivity.Idle { ac.addrs = addrs return true } - // Unless we're busy reconnecting already, let's reconnect from the top of - // the list. - if ac.state != connectivity.Ready { + if ac.state == connectivity.Connecting { return false } + // ac.state is Ready, try to find the connected address. var curAddrFound bool for _, a := range addrs { if reflect.DeepEqual(ac.curAddr, a) { @@ -1035,6 +1044,9 @@ func (ac *addrConn) resetTransport() { // The spec doesn't mention what should be done for multiple addresses. // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md#proposed-backoff-algorithm connectDeadline := time.Now().Add(dialDuration) + + ac.updateConnectivityState(connectivity.Connecting) + ac.transport = nil ac.mu.Unlock() newTr, addr, reconnect, err := ac.tryAllAddrs(addrs, connectDeadline) @@ -1131,8 +1143,6 @@ func (ac *addrConn) tryAllAddrs(addrs []resolver.Address, connectDeadline time.T ac.mu.Unlock() return nil, resolver.Address{}, nil, errConnClosing } - ac.updateConnectivityState(connectivity.Connecting) - ac.transport = nil ac.cc.mu.RLock() ac.dopts.copts.KeepaliveParams = ac.cc.mkp From 96dc2629b0d2b3400779198344c83f3002c7ea2d Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Wed, 29 May 2019 16:09:54 -0700 Subject: [PATCH 2/2] -s --- clientconn.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clientconn.go b/clientconn.go index b5e69c75c54e..880f2e499ccd 100644 --- a/clientconn.go +++ b/clientconn.go @@ -767,8 +767,8 @@ func (ac *addrConn) connect() error { // tryUpdateAddrs tries to update ac.addrs with the new addresses list. // -// If ac is Connecting, it returnes false. The caller should tear down the ac -// and create a new one. Note that the backoff will be reset when this happens. +// If ac is Connecting, it returns false. The caller should tear down the ac and +// create a new one. Note that the backoff will be reset when this happens. // // If ac is TransientFailure, it updates ac.addrs and returns true. The updated // addresses will be picked up by retry in the next iteration after backoff.