This repository has been archived by the owner on Jul 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
balancer.go
151 lines (125 loc) · 2.67 KB
/
balancer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package grpclb
import (
"io"
"sync"
"time"
"golang.org/x/net/context"
pb "github.com/bsm/grpclb/grpclb_balancer_v1"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/status"
)
// PickFirst returns a Balancer that always selects the first address returned
// from the Resolver
func PickFirst(opt *Options) grpc.Balancer {
if opt == nil {
opt = new(Options)
}
return &pickFirst{
nfy: make(chan []grpc.Address, 3),
opt: opt.norm(),
}
}
type pickFirst struct {
target string
opt *Options
addrs []grpc.Address
nfy chan []grpc.Address
mu sync.Mutex
cc io.Closer
lb pb.LoadBalancerClient
closing, closed chan struct{}
}
func (p *pickFirst) Start(target string, _ grpc.BalancerConfig) error {
cc, err := grpc.Dial(p.opt.Address, p.opt.DialOptions...)
if err != nil {
return err
}
p.target = target
p.cc = cc
p.lb = pb.NewLoadBalancerClient(cc)
p.closing = make(chan struct{})
p.closed = make(chan struct{})
go p.loop()
return nil
}
func (p *pickFirst) Up(_ grpc.Address) func(error) { return nil }
func (p *pickFirst) Notify() <-chan []grpc.Address { return p.nfy }
func (p *pickFirst) Get(ctx context.Context, opts grpc.BalancerGetOptions) (addr grpc.Address, _ func(), err error) {
if first, ok := p.first(); ok {
addr = first
return
} else if !opts.BlockingWait || p.closing == nil {
return
}
ticker := time.NewTicker(p.opt.UpdateInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
err = status.FromContextError(ctx.Err()).Err()
return
case <-p.closing:
err = grpc.ErrClientConnClosing
return
case <-ticker.C:
if first, ok := p.first(); ok {
addr = first
return
}
}
}
}
func (p *pickFirst) Close() (err error) {
if p.closing != nil {
close(p.closing)
<-p.closed
p.closing = nil
}
if p.cc != nil {
err = p.cc.Close()
p.cc = nil
}
return
}
func (p *pickFirst) first() (_ grpc.Address, _ bool) {
p.mu.Lock()
addrs := p.addrs
p.mu.Unlock()
if len(addrs) != 0 {
return addrs[0], true
}
return
}
func (p *pickFirst) update() error {
resp, err := p.lb.Servers(context.Background(), &pb.ServersRequest{
Target: p.target,
})
if err != nil {
return err
}
addrs := make([]grpc.Address, len(resp.Servers))
for i, s := range resp.Servers {
addrs[i].Addr = s.Address
}
p.mu.Lock()
p.addrs = addrs
p.mu.Unlock()
p.nfy <- addrs
return nil
}
func (p *pickFirst) loop() {
ticker := time.NewTicker(p.opt.UpdateInterval)
defer ticker.Stop()
for {
select {
case <-p.closing:
close(p.closed)
return
case <-ticker.C:
if err := p.update(); err != nil {
grpclog.Printf("Failed PickFirst balancer lookup: %s", err.Error())
}
}
}
}