refreshSubConns creates/removes SubConns with backendAddrs, and refreshes balancer state and picker. Caller must hold lb.mu.
(backendAddrs []resolver.Address, fallback bool, pickFirst bool)
| 106 | // |
| 107 | // Caller must hold lb.mu. |
| 108 | func (lb *lbBalancer) refreshSubConns(backendAddrs []resolver.Address, fallback bool, pickFirst bool) { |
| 109 | opts := balancer.NewSubConnOptions{} |
| 110 | if !fallback { |
| 111 | opts.CredsBundle = lb.grpclbBackendCreds |
| 112 | } |
| 113 | |
| 114 | lb.backendAddrs = backendAddrs |
| 115 | lb.backendAddrsWithoutMetadata = nil |
| 116 | |
| 117 | fallbackModeChanged := lb.inFallback != fallback |
| 118 | lb.inFallback = fallback |
| 119 | if fallbackModeChanged && lb.inFallback { |
| 120 | // Clear previous received list when entering fallback, so if the server |
| 121 | // comes back and sends the same list again, the new addresses will be |
| 122 | // used. |
| 123 | lb.fullServerList = nil |
| 124 | } |
| 125 | |
| 126 | balancingPolicyChanged := lb.usePickFirst != pickFirst |
| 127 | lb.usePickFirst = pickFirst |
| 128 | |
| 129 | if fallbackModeChanged || balancingPolicyChanged { |
| 130 | // Remove all SubConns when switching balancing policy or switching |
| 131 | // fallback mode. |
| 132 | // |
| 133 | // For fallback mode switching with pickfirst, we want to recreate the |
| 134 | // SubConn because the creds could be different. |
| 135 | for a, sc := range lb.subConns { |
| 136 | sc.Shutdown() |
| 137 | delete(lb.subConns, a) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if lb.usePickFirst { |
| 142 | var ( |
| 143 | scKey resolver.Address |
| 144 | sc balancer.SubConn |
| 145 | ) |
| 146 | for scKey, sc = range lb.subConns { |
| 147 | break |
| 148 | } |
| 149 | if sc != nil { |
| 150 | if len(backendAddrs) == 0 { |
| 151 | sc.Shutdown() |
| 152 | delete(lb.subConns, scKey) |
| 153 | return |
| 154 | } |
| 155 | lb.cc.ClientConn.UpdateAddresses(sc, backendAddrs) |
| 156 | sc.Connect() |
| 157 | return |
| 158 | } |
| 159 | opts.StateListener = func(scs balancer.SubConnState) { lb.updateSubConnState(sc, scs) } |
| 160 | // This bypasses the cc wrapper with SubConn cache. |
| 161 | sc, err := lb.cc.ClientConn.NewSubConn(backendAddrs, opts) |
| 162 | if err != nil { |
| 163 | lb.logger.Warningf("Failed to create new SubConn: %v", err) |
| 164 | return |
| 165 | } |
no test coverage detected