-
Notifications
You must be signed in to change notification settings - Fork 0
/
picker.go
310 lines (226 loc) · 4.5 KB
/
picker.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package picker
import (
"fmt"
"sync"
"time"
"github.com/golang/glog"
)
const (
Invalid = -1
)
var defaultPeer = Peer{
Weight: 1,
MaxFails: 1,
FailTimeout: 10 * time.Second,
}
type CheckFunc func(i int, data interface{}) bool
type context struct {
Tries int
CheckFunc CheckFunc
tried []bool
picker *Picker
current *Peer
}
type Picker struct {
sync.RWMutex
CheckFunc CheckFunc
peers []*Peer
inited bool
}
// New 创建一个新的 Picker
func New(n int) *Picker {
p := &Picker{}
if n > 0 {
p.peers = make([]*Peer, n)
for i := 0; i < n; i++ {
peer := defaultPeer
peer.index = i
peer.Name = fmt.Sprintf("peer_%v", i)
p.peers[i] = &peer
}
}
return p
}
func (p *Picker) Get(i int) *Peer {
return p.peers[i]
}
func (p *Picker) Add() *Peer {
peer := defaultPeer
peer.index = len(p.peers)
peer.Name = fmt.Sprintf("peer_%v", peer.index)
p.peers = append(p.peers, &peer)
return &peer
}
func (p *Picker) Context() *context {
return &context{
picker: p,
Tries: len(p.peers),
}
}
type Peer struct {
sync.Mutex
Name string
Weight int64
MaxFails uint64
FailTimeout time.Duration
MaxConnections uint64
Data interface{}
index int
currentWeight int64
effectiveWeight int64
connections uint64
fails uint64
accessed time.Time
checked time.Time
}
func (p *Picker) init() {
for _, peer := range p.peers {
peer.effectiveWeight = peer.Weight
}
p.inited = true
}
func (p *Picker) pick(ctx *context) *Peer {
var (
best *Peer
index int
total int64
)
if ctx.tried == nil {
ctx.tried = make([]bool, len(p.peers))
}
for i, peer := range p.peers {
if ctx.tried[i] {
continue
}
if ctx.CheckFunc != nil && !ctx.CheckFunc(i, peer.Data) {
continue
}
if p.CheckFunc != nil && !p.CheckFunc(i, peer.Data) {
continue
}
if peer.MaxConnections > 0 && peer.connections >= peer.MaxConnections {
continue
}
if peer.MaxFails > 0 && peer.fails >= peer.MaxFails {
if time.Since(peer.checked) <= peer.FailTimeout {
continue
}
glog.Infof("try failed peer %v", peer.Name)
}
peer.currentWeight += peer.effectiveWeight
total += peer.effectiveWeight
if peer.effectiveWeight < peer.Weight {
peer.effectiveWeight++
}
if best == nil || peer.currentWeight > best.currentWeight {
best = peer
index = i
}
}
if best == nil {
return nil
}
ctx.tried[index] = true
best.currentWeight -= total
if time.Since(best.checked) > best.FailTimeout {
best.checked = time.Now()
}
return best
}
func (p *Picker) Pick(ctx *context) *Peer {
if ctx.current != nil {
glog.Fatalf("previous peer not released: %v", ctx.current)
}
if ctx.Tries == 0 {
return nil
}
p.Lock()
defer p.Unlock()
if len(p.peers) == 0 {
return nil
}
if !p.inited {
p.init()
}
var peer *Peer
if len(p.peers) == 1 {
peer = p.peers[0]
if peer.MaxConnections > 0 && peer.connections >= peer.MaxConnections {
goto failed
}
if ctx.CheckFunc != nil && !ctx.CheckFunc(0, peer.Data) {
goto failed
}
} else {
peer = p.pick(ctx)
if peer == nil {
goto failed
}
glog.Infof("get rr peer, current: %v %v", peer.Name, peer.currentWeight)
}
peer.connections++
ctx.current = peer
return peer
failed:
return nil
}
func (p *Picker) release(ctx *context, ok bool) {
peer := ctx.current
if peer == nil {
glog.Fatal("peer release with no current")
return
}
p.RLock()
peer.Lock()
if len(p.peers) == 1 {
peer.connections--
peer.Unlock()
p.RUnlock()
ctx.Tries = 0
ctx.current = nil
return
}
if !ok {
now := time.Now()
peer.fails++
peer.accessed = now
peer.checked = now
if peer.MaxFails > 0 {
peer.effectiveWeight -= peer.Weight / int64(peer.MaxFails)
if peer.fails >= peer.MaxFails {
glog.Errorf("peer %v temporarily disabled", peer.Name)
}
}
glog.Infof("free failed peer: %v, %v", peer.Name, peer.effectiveWeight)
if peer.effectiveWeight < 0 {
peer.effectiveWeight = 0
}
} else {
if peer.accessed.Before(peer.checked) {
peer.fails = 0
glog.Infof("peer %v is up", peer.Name)
}
}
peer.connections--
peer.Unlock()
p.RUnlock()
if ctx.Tries > 0 {
ctx.Tries--
}
ctx.current = nil
}
func (c *context) Pick() (int, interface{}) {
peer := c.picker.Pick(c)
if peer == nil {
return Invalid, nil
}
return peer.index, peer.Data
}
func (c *context) Release(ok bool) {
c.picker.release(c, ok)
}
func (c *context) Close() {
if c.current != nil {
glog.Fatalf("last peer not released in picker. %#v", c.current)
}
}