* * 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 grpclb

import (
	
	

	
	lbpb 
	
	
	
)
rpcStats is same as lbpb.ClientStats, except that numCallsDropped is a map instead of a slice.
map load_balance_token -> num_calls_dropped
Pick always returns this err.
rrPicker does roundrobin on subConns. It's typically used when there's no response from remote balancer, and grpclb falls back to the resolved backends. It guaranteed that len(subConns) > 0.
type rrPicker struct {
	mu           sync.Mutex
	subConns     []balancer.SubConn // The subConns that were READY when taking the snapshot.
	subConnsNext int
}

func ( []balancer.SubConn) *rrPicker {
	return &rrPicker{
		subConns:     ,
		subConnsNext: grpcrand.Intn(len()),
	}
}

func ( *rrPicker) (balancer.PickInfo) (balancer.PickResult, error) {
	.mu.Lock()
	defer .mu.Unlock()
	 := .subConns[.subConnsNext]
	.subConnsNext = (.subConnsNext + 1) % len(.subConns)
	return balancer.PickResult{SubConn: }, nil
}
lbPicker does two layers of picks: First layer: roundrobin on all servers in serverList, including drops and backends. - If it picks a drop, the RPC will fail as being dropped. - If it picks a backend, do a second layer pick to pick the real backend. Second layer: roundrobin on all READY backends. It's guaranteed that len(serverList) > 0.
type lbPicker struct {
	mu             sync.Mutex
	serverList     []*lbpb.Server
	serverListNext int
	subConns       []balancer.SubConn // The subConns that were READY when taking the snapshot.
	subConnsNext   int

	stats *rpcStats
}

func ( []*lbpb.Server,  []balancer.SubConn,  *rpcStats) *lbPicker {
	return &lbPicker{
		serverList:   ,
		subConns:     ,
		subConnsNext: grpcrand.Intn(len()),
		stats:        ,
	}
}

func ( *lbPicker) (balancer.PickInfo) (balancer.PickResult, error) {
	.mu.Lock()
	defer .mu.Unlock()
Layer one roundrobin on serverList.
If it's a drop, return an error and fail the RPC.
	if .Drop {
		.stats.drop(.LoadBalanceToken)
		return balancer.PickResult{}, status.Errorf(codes.Unavailable, "request dropped by grpclb")
	}
If not a drop but there's no ready subConns.
Return the next ready subConn in the list, also collect rpc stats.
	 := .subConns[.subConnsNext]
	.subConnsNext = (.subConnsNext + 1) % len(.subConns)
	 := func( balancer.DoneInfo) {
		if !.BytesSent {
			.stats.failedToSend()
		} else if .BytesReceived {
			.stats.knownReceived()
		}
	}
	return balancer.PickResult{SubConn: , Done: }, nil
}

func ( *lbPicker) ( []balancer.SubConn) {
	.mu.Lock()
	defer .mu.Unlock()

	.subConns = 
	.subConnsNext = .subConnsNext % len()