blob: d4d645501c14c600bb52b86708e0538a8f01418b [file] [log] [blame]
Scott Baker105df152020-04-13 15:55:14 -07001/*
2 *
3 * Copyright 2017 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
19// Package roundrobin defines a roundrobin balancer. Roundrobin balancer is
20// installed as one of the default balancers in gRPC, users don't need to
21// explicitly install this balancer.
22package roundrobin
23
24import (
25 "sync"
26
27 "google.golang.org/grpc/balancer"
28 "google.golang.org/grpc/balancer/base"
29 "google.golang.org/grpc/grpclog"
30 "google.golang.org/grpc/internal/grpcrand"
31)
32
33// Name is the name of round_robin balancer.
34const Name = "round_robin"
35
36// newBuilder creates a new roundrobin balancer builder.
37func newBuilder() balancer.Builder {
38 return base.NewBalancerBuilderV2(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true})
39}
40
41func init() {
42 balancer.Register(newBuilder())
43}
44
45type rrPickerBuilder struct{}
46
47func (*rrPickerBuilder) Build(info base.PickerBuildInfo) balancer.V2Picker {
48 grpclog.Infof("roundrobinPicker: newPicker called with info: %v", info)
49 if len(info.ReadySCs) == 0 {
50 return base.NewErrPickerV2(balancer.ErrNoSubConnAvailable)
51 }
52 var scs []balancer.SubConn
53 for sc := range info.ReadySCs {
54 scs = append(scs, sc)
55 }
56 return &rrPicker{
57 subConns: scs,
58 // Start at a random index, as the same RR balancer rebuilds a new
59 // picker when SubConn states change, and we don't want to apply excess
60 // load to the first server in the list.
61 next: grpcrand.Intn(len(scs)),
62 }
63}
64
65type rrPicker struct {
66 // subConns is the snapshot of the roundrobin balancer when this picker was
67 // created. The slice is immutable. Each Get() will do a round robin
68 // selection from it and return the selected SubConn.
69 subConns []balancer.SubConn
70
71 mu sync.Mutex
72 next int
73}
74
75func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
76 p.mu.Lock()
77 sc := p.subConns[p.next]
78 p.next = (p.next + 1) % len(p.subConns)
79 p.mu.Unlock()
80 return balancer.PickResult{SubConn: sc}, nil
81}