* * Copyright 2017 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *

package grpc

import (
	
	
	

	
	
	
	
	
)
pickerWrapper is a wrapper of balancer.Picker. It blocks on certain pick actions and unblock when there's a picker update.
type pickerWrapper struct {
	mu         sync.Mutex
	done       bool
	blockingCh chan struct{}
	picker     balancer.Picker
}

func () *pickerWrapper {
	return &pickerWrapper{blockingCh: make(chan struct{})}
}
updatePicker is called by UpdateBalancerState. It unblocks all blocked pick.
func ( *pickerWrapper) ( balancer.Picker) {
	.mu.Lock()
	if .done {
		.mu.Unlock()
		return
	}
pw.blockingCh should never be nil.
	close(.blockingCh)
	.blockingCh = make(chan struct{})
	.mu.Unlock()
}

func ( *acBalancerWrapper,  func(balancer.DoneInfo)) func(balancer.DoneInfo) {
	.mu.Lock()
	 := .ac
	.mu.Unlock()
	.incrCallsStarted()
	return func( balancer.DoneInfo) {
		if .Err != nil && .Err != io.EOF {
			.incrCallsFailed()
		} else {
			.incrCallsSucceeded()
		}
		if  != nil {
			()
		}
	}
}
pick returns the transport that will be used for the RPC. It may block in the following cases: - there's no picker - the current picker returns ErrNoSubConnAvailable - the current picker returns other errors and failfast is false. - the subConn returned by the current picker is not READY When one of these situations happens, pick blocks until the picker gets updated.
func ( *pickerWrapper) ( context.Context,  bool,  balancer.PickInfo) (transport.ClientTransport, func(balancer.DoneInfo), error) {
	var  chan struct{}

	var  error
	for {
		.mu.Lock()
		if .done {
			.mu.Unlock()
			return nil, nil, ErrClientConnClosing
		}

		if .picker == nil {
			 = .blockingCh
		}
This could happen when either: - pw.picker is nil (the previous if condition), or - has called pick on the current picker.
			.mu.Unlock()
			select {
			case <-.Done():
				var  string
				if  != nil {
					 = "latest balancer error: " + .Error()
				} else {
					 = .Err().Error()
				}
				switch .Err() {
				case context.DeadlineExceeded:
					return nil, nil, status.Error(codes.DeadlineExceeded, )
				case context.Canceled:
					return nil, nil, status.Error(codes.Canceled, )
				}
			case <-:
			}
			continue
		}

		 = .blockingCh
		 := .picker
		.mu.Unlock()

		,  := .Pick()

		if  != nil {
			if  == balancer.ErrNoSubConnAvailable {
				continue
			}
Status error: end the RPC unconditionally with this status.
				return nil, nil, 
For all other errors, wait for ready RPCs should block and other RPCs should fail with unavailable.
			if ! {
				 = 
				continue
			}
			return nil, nil, status.Error(codes.Unavailable, .Error())
		}

		,  := .SubConn.(*acBalancerWrapper)
		if ! {
			logger.Error("subconn returned from pick is not *acBalancerWrapper")
			continue
		}
		if ,  := .getAddrConn().getReadyTransport();  {
			if channelz.IsOn() {
				return , doneChannelzWrapper(, .Done), nil
			}
			return , .Done, nil
		}
Calling done with nil error, no bytes sent and no bytes received. DoneInfo with default value works.
			.Done(balancer.DoneInfo{})
		}
If ok == false, ac.state is not READY. A valid picker always returns READY subConn. This means the state of ac just changed, and picker will be updated shortly. continue back to the beginning of the for loop to repick.
	}
}

func ( *pickerWrapper) () {
	.mu.Lock()
	defer .mu.Unlock()
	if .done {
		return
	}
	.done = true
	close(.blockingCh)