Skip to content

Commit

Permalink
addrConn: not reset backoff if update address in TransientFailure (#2740
Browse files Browse the repository at this point in the history
)
  • Loading branch information
menghanl authored May 30, 2019
1 parent 58c136c commit 82fdf27
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,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.
Expand All @@ -745,25 +744,35 @@ 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 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.
//
// 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.
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) {
Expand Down Expand Up @@ -1012,6 +1021,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)
Expand Down Expand Up @@ -1108,8 +1120,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
Expand Down

0 comments on commit 82fdf27

Please sign in to comment.