blob: ed05b02ed96e0873d122fe33e501b73877b86b58 [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -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
19package grpc
20
21import (
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.
31const PickFirstBalancerName = "pick_first"
32
33func newPickfirstBuilder() balancer.Builder {
34 return &pickfirstBuilder{}
35}
36
37type pickfirstBuilder struct{}
38
39func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
40 return &pickfirstBalancer{cc: cc}
41}
42
43func (*pickfirstBuilder) Name() string {
44 return PickFirstBalancerName
45}
46
47type pickfirstBalancer struct {
48 cc balancer.ClientConn
49 sc balancer.SubConn
50}
51
52func (b *pickfirstBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
53 if err != nil {
Scott Baker8487c5d2019-10-18 12:49:46 -070054 if grpclog.V(2) {
55 grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err)
56 }
Scott Baker2d897982019-09-24 11:50:08 -070057 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 Baker8487c5d2019-10-18 12:49:46 -070063 if grpclog.V(2) {
64 grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
65 }
Scott Baker2d897982019-09-24 11:50:08 -070066 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
76func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
Scott Baker8487c5d2019-10-18 12:49:46 -070077 if grpclog.V(2) {
78 grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s)
79 }
Scott Baker2d897982019-09-24 11:50:08 -070080 if b.sc != sc {
Scott Baker8487c5d2019-10-18 12:49:46 -070081 if grpclog.V(2) {
82 grpclog.Infof("pickfirstBalancer: ignored state change because sc is not recognized")
83 }
Scott Baker2d897982019-09-24 11:50:08 -070084 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
101func (b *pickfirstBalancer) Close() {
102}
103
104type picker struct {
105 err error
106 sc balancer.SubConn
107}
108
109func (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
116func init() {
117 balancer.Register(newPickfirstBuilder())
118}