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

Method UpdateState

balancer/leastrequest/leastrequest.go:153–211  ·  view source on GitHub ↗
(state balancer.State)

Source from the content-addressed store, hash-verified

151}
152
153func (lrb *leastRequestBalancer) UpdateState(state balancer.State) {
154 var readyEndpoints []endpointsharding.ChildState
155 for _, child := range endpointsharding.ChildStatesFromPicker(state.Picker) {
156 if child.State.ConnectivityState == connectivity.Ready {
157 readyEndpoints = append(readyEndpoints, child)
158 }
159 }
160
161 // If no ready pickers are present, simply defer to the round robin picker
162 // from endpoint sharding, which will round robin across the most relevant
163 // pick first children in the highest precedence connectivity state.
164 if len(readyEndpoints) == 0 {
165 lrb.ClientConn.UpdateState(state)
166 return
167 }
168
169 lrb.mu.Lock()
170 defer lrb.mu.Unlock()
171
172 if logger.V(2) {
173 lrb.logger.Infof("UpdateState called with ready endpoints: %v", readyEndpoints)
174 }
175
176 // Reconcile endpoints.
177 newEndpoints := resolver.NewEndpointMap[any]()
178 for _, child := range readyEndpoints {
179 newEndpoints.Set(child.Endpoint, nil)
180 }
181
182 // If endpoints are no longer ready, no need to count their active RPCs.
183 for endpoint := range lrb.endpointRPCCounts.All() {
184 if _, ok := newEndpoints.Get(endpoint); !ok {
185 lrb.endpointRPCCounts.Delete(endpoint)
186 }
187 }
188
189 // Copy refs to counters into picker.
190 endpointStates := make([]endpointState, 0, len(readyEndpoints))
191 for _, child := range readyEndpoints {
192 counter, ok := lrb.endpointRPCCounts.Get(child.Endpoint)
193 if !ok {
194 // Create new counts if needed.
195 counter = new(atomic.Int32)
196 lrb.endpointRPCCounts.Set(child.Endpoint, counter)
197 }
198 endpointStates = append(endpointStates, endpointState{
199 picker: child.State.Picker,
200 numRPCs: counter,
201 })
202 }
203
204 lrb.ClientConn.UpdateState(balancer.State{
205 Picker: &picker{
206 choiceCount: lrb.choiceCount,
207 endpointStates: endpointStates,
208 },
209 ConnectivityState: connectivity.Ready,
210 })

Callers

nothing calls this directly

Calls 10

ChildStatesFromPickerFunction · 0.92
UpdateStateMethod · 0.65
VMethod · 0.65
InfofMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45
AllMethod · 0.45
DeleteMethod · 0.45

Tested by

no test coverage detected