Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 1 | /* |
| 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. |
| 22 | package roundrobin |
| 23 | |
| 24 | import ( |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 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" |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | // Name is the name of round_robin balancer. |
| 34 | const Name = "round_robin" |
| 35 | |
| 36 | // newBuilder creates a new roundrobin balancer builder. |
| 37 | func newBuilder() balancer.Builder { |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 38 | return base.NewBalancerBuilderV2(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true}) |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | func init() { |
| 42 | balancer.Register(newBuilder()) |
| 43 | } |
| 44 | |
| 45 | type rrPickerBuilder struct{} |
| 46 | |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 47 | func (*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) |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 51 | } |
| 52 | var scs []balancer.SubConn |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 53 | for sc := range info.ReadySCs { |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 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 | |
| 65 | type 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 | |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 75 | func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) { |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 76 | p.mu.Lock() |
| 77 | sc := p.subConns[p.next] |
| 78 | p.next = (p.next + 1) % len(p.subConns) |
| 79 | p.mu.Unlock() |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 80 | return balancer.PickResult{SubConn: sc}, nil |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 81 | } |