updateState is invoked by grpc to push a subConn state update to the underlying balancer.
(s connectivity.State, err error)
| 305 | // updateState is invoked by grpc to push a subConn state update to the |
| 306 | // underlying balancer. |
| 307 | func (acbw *acBalancerWrapper) updateState(s connectivity.State, err error) { |
| 308 | acbw.ccb.serializer.TrySchedule(func(ctx context.Context) { |
| 309 | if ctx.Err() != nil || acbw.ccb.balancer == nil { |
| 310 | return |
| 311 | } |
| 312 | // Invalidate all producers on any state change. |
| 313 | acbw.closeProducers() |
| 314 | |
| 315 | // Even though it is optional for balancers, gracefulswitch ensures |
| 316 | // opts.StateListener is set, so this cannot ever be nil. |
| 317 | // TODO: delete this comment when UpdateSubConnState is removed. |
| 318 | scs := balancer.SubConnState{ConnectivityState: s, ConnectionError: err} |
| 319 | // Invalidate the health listener by updating the healthData. |
| 320 | acbw.healthMu.Lock() |
| 321 | // A race may occur if a health listener is registered soon after the |
| 322 | // connectivity state is set but before the stateListener is called. |
| 323 | // Two cases may arise: |
| 324 | // 1. The new state is not READY: RegisterHealthListener has checks to |
| 325 | // ensure no updates are sent when the connectivity state is not |
| 326 | // READY. |
| 327 | // 2. The new state is READY: This means that the old state wasn't Ready. |
| 328 | // The RegisterHealthListener API mentions that a health listener |
| 329 | // must not be registered when a SubConn is not ready to avoid such |
| 330 | // races. When this happens, the LB policy would get health updates |
| 331 | // on the old listener. When the LB policy registers a new listener |
| 332 | // on receiving the connectivity update, the health updates will be |
| 333 | // sent to the new health listener. |
| 334 | acbw.healthData = newHealthData(scs.ConnectivityState) |
| 335 | acbw.healthMu.Unlock() |
| 336 | |
| 337 | acbw.stateListener(scs) |
| 338 | }) |
| 339 | } |
| 340 | |
| 341 | func (acbw *acBalancerWrapper) String() string { |
| 342 | return fmt.Sprintf("SubConn(id:%d)", acbw.ac.channelz.ID) |
no test coverage detected