updateAddrs updates ac.addrs with the new addresses list and handles active connections or connection attempts.
(addrs []resolver.Address)
| 1016 | // updateAddrs updates ac.addrs with the new addresses list and handles active |
| 1017 | // connections or connection attempts. |
| 1018 | func (ac *addrConn) updateAddrs(addrs []resolver.Address) { |
| 1019 | addrs = copyAddresses(addrs) |
| 1020 | limit := len(addrs) |
| 1021 | if limit > 5 { |
| 1022 | limit = 5 |
| 1023 | } |
| 1024 | channelz.Infof(logger, ac.channelz, "addrConn: updateAddrs addrs (%d of %d): %v", limit, len(addrs), addrs[:limit]) |
| 1025 | |
| 1026 | ac.mu.Lock() |
| 1027 | if equalAddressesIgnoringBalAttributes(ac.addrs, addrs) { |
| 1028 | ac.mu.Unlock() |
| 1029 | return |
| 1030 | } |
| 1031 | |
| 1032 | ac.addrs = addrs |
| 1033 | ac.updateTelemetryLabelsLocked() |
| 1034 | if ac.state == connectivity.Shutdown || |
| 1035 | ac.state == connectivity.TransientFailure || |
| 1036 | ac.state == connectivity.Idle { |
| 1037 | // We were not connecting, so do nothing but update the addresses. |
| 1038 | ac.mu.Unlock() |
| 1039 | return |
| 1040 | } |
| 1041 | |
| 1042 | if ac.state == connectivity.Ready { |
| 1043 | // Try to find the connected address. |
| 1044 | for _, a := range addrs { |
| 1045 | a.ServerName = ac.cc.getServerName(a) |
| 1046 | if equalAddressIgnoringBalAttributes(&a, &ac.curAddr) { |
| 1047 | // We are connected to a valid address, so do nothing but |
| 1048 | // update the addresses. |
| 1049 | ac.mu.Unlock() |
| 1050 | return |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | // We are either connected to the wrong address or currently connecting. |
| 1056 | // Stop the current iteration and restart. |
| 1057 | |
| 1058 | ac.cancel() |
| 1059 | ac.ctx, ac.cancel = context.WithCancel(ac.cc.ctx) |
| 1060 | |
| 1061 | // We have to defer here because GracefulClose => onClose, which requires |
| 1062 | // locking ac.mu. |
| 1063 | if ac.transport != nil { |
| 1064 | defer ac.transport.GracefulClose() |
| 1065 | ac.transport = nil |
| 1066 | } |
| 1067 | |
| 1068 | if len(addrs) == 0 { |
| 1069 | ac.updateConnectivityState(connectivity.Idle, nil) |
| 1070 | } |
| 1071 | |
| 1072 | // Since we were connecting/connected, we should start a new connection |
| 1073 | // attempt. |
| 1074 | go ac.resetTransportAndUnlock() |
| 1075 | } |
no test coverage detected