1/*
2 *
3 * Copyright 2020 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package xdsrouting
20
21import (
22	"google.golang.org/grpc/balancer"
23	"google.golang.org/grpc/codes"
24	"google.golang.org/grpc/status"
25	"google.golang.org/grpc/xds/internal"
26)
27
28// pickerGroup contains a list of route matchers and their corresponding
29// pickers. For each pick, the first matched picker is used. If the picker isn't
30// ready, the pick will be queued.
31type pickerGroup struct {
32	routes  []route
33	pickers map[string]balancer.Picker
34}
35
36func newPickerGroup(routes []route, idToPickerState map[internal.LocalityID]*subBalancerState) *pickerGroup {
37	pickers := make(map[string]balancer.Picker)
38	for id, st := range idToPickerState {
39		pickers[getNameFromLocality(id)] = st.state.Picker
40	}
41	return &pickerGroup{
42		routes:  routes,
43		pickers: pickers,
44	}
45}
46
47var errNoMatchedRouteFound = status.Errorf(codes.Unavailable, "no matched route was found")
48
49func (pg *pickerGroup) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
50	for _, rt := range pg.routes {
51		if rt.m.match(info) {
52			// action from route is the ID for the sub-balancer to use.
53			p, ok := pg.pickers[rt.action]
54			if !ok {
55				return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
56			}
57			return p.Pick(info)
58		}
59	}
60	return balancer.PickResult{}, errNoMatchedRouteFound
61}
62