MCPcopy
hub / github.com/grpc/grpc-go / refreshSubConns

Method refreshSubConns

balancer/grpclb/grpclb_remote_balancer.go:108–216  ·  view source on GitHub ↗

refreshSubConns creates/removes SubConns with backendAddrs, and refreshes balancer state and picker. Caller must hold lb.mu.

(backendAddrs []resolver.Address, fallback bool, pickFirst bool)

Source from the content-addressed store, hash-verified

106//
107// Caller must hold lb.mu.
108func (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 }

Callers 7

updateSubConnStateMethod · 0.95
handleServiceConfigMethod · 0.95
UpdateClientConnStateMethod · 0.95
processServerListMethod · 0.95
readServerListMethod · 0.80
watchRemoteBalancerMethod · 0.80

Calls 7

ShutdownMethod · 0.95
ConnectMethod · 0.95
updateSubConnStateMethod · 0.95
updateStateAndPickerMethod · 0.95
UpdateAddressesMethod · 0.65
NewSubConnMethod · 0.65
WarningfMethod · 0.65

Tested by

no test coverage detected