Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [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 grpc |
| 20 | |
| 21 | import ( |
| 22 | "context" |
| 23 | |
| 24 | "google.golang.org/grpc/balancer" |
| 25 | "google.golang.org/grpc/connectivity" |
| 26 | "google.golang.org/grpc/grpclog" |
| 27 | "google.golang.org/grpc/resolver" |
| 28 | ) |
| 29 | |
| 30 | // PickFirstBalancerName is the name of the pick_first balancer. |
| 31 | const PickFirstBalancerName = "pick_first" |
| 32 | |
| 33 | func newPickfirstBuilder() balancer.Builder { |
| 34 | return &pickfirstBuilder{} |
| 35 | } |
| 36 | |
| 37 | type pickfirstBuilder struct{} |
| 38 | |
| 39 | func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer { |
| 40 | return &pickfirstBalancer{cc: cc} |
| 41 | } |
| 42 | |
| 43 | func (*pickfirstBuilder) Name() string { |
| 44 | return PickFirstBalancerName |
| 45 | } |
| 46 | |
| 47 | type pickfirstBalancer struct { |
| 48 | cc balancer.ClientConn |
| 49 | sc balancer.SubConn |
| 50 | } |
| 51 | |
| 52 | func (b *pickfirstBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) { |
| 53 | if err != nil { |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame] | 54 | if grpclog.V(2) { |
| 55 | grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err) |
| 56 | } |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 57 | return |
| 58 | } |
| 59 | if b.sc == nil { |
| 60 | b.sc, err = b.cc.NewSubConn(addrs, balancer.NewSubConnOptions{}) |
| 61 | if err != nil { |
| 62 | //TODO(yuxuanli): why not change the cc state to Idle? |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame] | 63 | if grpclog.V(2) { |
| 64 | grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err) |
| 65 | } |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 66 | return |
| 67 | } |
| 68 | b.cc.UpdateBalancerState(connectivity.Idle, &picker{sc: b.sc}) |
| 69 | b.sc.Connect() |
| 70 | } else { |
| 71 | b.sc.UpdateAddresses(addrs) |
| 72 | b.sc.Connect() |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) { |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame] | 77 | if grpclog.V(2) { |
| 78 | grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s) |
| 79 | } |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 80 | if b.sc != sc { |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame] | 81 | if grpclog.V(2) { |
| 82 | grpclog.Infof("pickfirstBalancer: ignored state change because sc is not recognized") |
| 83 | } |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 84 | return |
| 85 | } |
| 86 | if s == connectivity.Shutdown { |
| 87 | b.sc = nil |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | switch s { |
| 92 | case connectivity.Ready, connectivity.Idle: |
| 93 | b.cc.UpdateBalancerState(s, &picker{sc: sc}) |
| 94 | case connectivity.Connecting: |
| 95 | b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable}) |
| 96 | case connectivity.TransientFailure: |
| 97 | b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure}) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func (b *pickfirstBalancer) Close() { |
| 102 | } |
| 103 | |
| 104 | type picker struct { |
| 105 | err error |
| 106 | sc balancer.SubConn |
| 107 | } |
| 108 | |
| 109 | func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) { |
| 110 | if p.err != nil { |
| 111 | return nil, nil, p.err |
| 112 | } |
| 113 | return p.sc, nil, nil |
| 114 | } |
| 115 | |
| 116 | func init() { |
| 117 | balancer.Register(newPickfirstBuilder()) |
| 118 | } |