| 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 |
"golang.org/x/net/context" |
| 23 |
"google.golang.org/grpc/balancer" |
| 24 |
"google.golang.org/grpc/connectivity" |
| 25 |
"google.golang.org/grpc/grpclog" |
| 26 |
"google.golang.org/grpc/resolver" |
| 27 |
) |
| 28 |
|
| 29 |
func newPickfirstBuilder() balancer.Builder { |
| 30 |
return &pickfirstBuilder{} |
| 31 |
} |
| 32 |
|
| 33 |
type pickfirstBuilder struct{} |
| 34 |
|
| 35 |
func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer { |
| 36 |
return &pickfirstBalancer{cc: cc} |
| 37 |
} |
| 38 |
|
| 39 |
func (*pickfirstBuilder) Name() string { |
| 40 |
return "pickfirst" |
| 41 |
} |
| 42 |
|
| 43 |
type pickfirstBalancer struct { |
| 44 |
cc balancer.ClientConn |
| 45 |
sc balancer.SubConn |
| 46 |
} |
| 47 |
|
| 48 |
func (b *pickfirstBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) { |
| 49 |
if err != nil { |
| 50 |
grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err) |
| 51 |
return |
| 52 |
} |
| 53 |
if b.sc == nil { |
| 54 |
b.sc, err = b.cc.NewSubConn(addrs, balancer.NewSubConnOptions{}) |
| 55 |
if err != nil { |
| 56 |
grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err) |
| 57 |
return |
| 58 |
} |
| 59 |
b.cc.UpdateBalancerState(connectivity.Idle, &picker{sc: b.sc}) |
| 60 |
} else { |
| 61 |
b.sc.UpdateAddresses(addrs) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) { |
| 66 |
grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s) |
| 67 |
if b.sc != sc || s == connectivity.Shutdown { |
| 68 |
b.sc = nil |
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
switch s { |
| 73 |
case connectivity.Ready, connectivity.Idle: |
| 74 |
b.cc.UpdateBalancerState(s, &picker{sc: sc}) |
| 75 |
case connectivity.Connecting: |
| 76 |
b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable}) |
| 77 |
case connectivity.TransientFailure: |
| 78 |
b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure}) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func (b *pickfirstBalancer) Close() { |
| 83 |
} |
| 84 |
|
| 85 |
type picker struct { |
| 86 |
err error |
| 87 |
sc balancer.SubConn |
| 88 |
} |
| 89 |
|
| 90 |
func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) { |
| 91 |
if p.err != nil { |
| 92 |
return nil, nil, p.err |
| 93 |
} |
| 94 |
return p.sc, nil, nil |
| 95 |
} |