func()

in grpcgcp/gcp_balancer.go [381:413]


func (gb *gcpBalancer) getSubConnRoundRobin(ctx context.Context) *subConnRef {
	if len(gb.scRefList) == 0 {
		gb.newSubConn()
	}
	scRef := gb.scRefList[atomic.AddUint32(&gb.rrRefId, 1)%uint32(len(gb.scRefList))]

	gb.mu.RLock()
	if state := gb.scStates[scRef.subConn]; state == connectivity.Ready {
		gb.mu.RUnlock()
		return scRef
	} else {
		grpclog.Infof("grpcgcp.gcpBalancer: scRef is not ready: %v", state)
	}

	ticker := time.NewTicker(time.Millisecond * 100)
	defer ticker.Stop()

	// Wait until SubConn is ready or call context is done.
	for gb.scStates[scRef.subConn] != connectivity.Ready {
		sigChan := scRef.stateSignal
		gb.mu.RUnlock()
		select {
		case <-ctx.Done():
			return scRef
		case <-ticker.C:
		case <-sigChan:
		}
		gb.mu.RLock()
	}
	gb.mu.RUnlock()

	return scRef
}