blob: ed05b02ed96e0873d122fe33e501b73877b86b58 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -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 {
divyadesai19009132020-03-04 12:58:08 +000054 if grpclog.V(2) {
55 grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err)
56 }
Zack Williamse940c7a2019-08-21 14:25:39 -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?
divyadesai19009132020-03-04 12:58:08 +000063 if grpclog.V(2) {
64 grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
65 }
Zack Williamse940c7a2019-08-21 14:25:39 -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) {
divyadesai19009132020-03-04 12:58:08 +000077 if grpclog.V(2) {
78 grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s)
79 }
Zack Williamse940c7a2019-08-21 14:25:39 -070080 if b.sc != sc {
divyadesai19009132020-03-04 12:58:08 +000081 if grpclog.V(2) {
82 grpclog.Infof("pickfirstBalancer: ignored state change because sc is not recognized")
83 }
Zack Williamse940c7a2019-08-21 14:25:39 -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}