forked from onestraw/golb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rr.go
212 lines (185 loc) · 3.62 KB
/
rr.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
package roundrobin
import (
"fmt"
"sort"
"strings"
"sync"
"sync/atomic"
)
// Peer represents a backend server.
type Peer struct {
addr string
weight int
effectiveWeight int
currentWeight int
down bool
sync.RWMutex
}
func (p *Peer) String() string {
p.RLock()
defer p.RUnlock()
return fmt.Sprintf("%s: (w=%d, ew=%d, cw=%d)",
p.addr, p.weight, p.effectiveWeight, p.currentWeight)
}
// CreatePeer return a Peer object.
func CreatePeer(addr string, weight int) *Peer {
return &Peer{
addr: addr,
weight: weight,
effectiveWeight: weight,
currentWeight: 0,
down: false,
}
}
// Pool is a group of Peers, one Peer can not belong to multiple Pool.
type Pool struct {
peers []*Peer
current uint64
downNum int
sync.RWMutex
}
func (p *Pool) String() string {
p.RLock()
defer p.RUnlock()
result := []string{}
for _, peer := range p.peers {
result = append(result, peer.addr)
}
sort.Strings(result)
return strings.Join(result, ", ")
}
// Size return the number of the peer.
func (p *Pool) Size() int {
return len(p.peers)
}
// Add append a peer to the pool if not exists.
func (p *Pool) Add(addr string, args ...interface{}) {
if addr == "" {
return
}
if idx := p.indexOfPeer(addr); idx >= 0 {
return
}
weight := 1
if len(args) > 0 {
if w, ok := args[0].(int); ok {
weight = w
}
}
peer := CreatePeer(addr, weight)
p.Lock()
defer p.Unlock()
if peer.down {
p.downNum++
}
p.peers = append(p.peers, peer)
}
func (p *Pool) indexOfPeer(addr string) int {
for i, peer := range p.peers {
if peer.addr == addr {
return i
}
}
return -1
}
func (p *Pool) setPeerStatus(addr string, isDown bool) {
p.RLock()
idx := p.indexOfPeer(addr)
p.RUnlock()
if idx >= 0 && idx < p.Size() {
peer := p.peers[idx]
if peer.down != isDown {
p.Lock()
if isDown {
p.downNum++
} else {
p.downNum--
}
p.Unlock()
peer.Lock()
peer.down = isDown
peer.Unlock()
}
}
}
// DownPeer mark the peer down.
func (p *Pool) DownPeer(addr string) {
p.setPeerStatus(addr, true)
}
// UpPeer mark the peer up.
func (p *Pool) UpPeer(addr string) {
p.setPeerStatus(addr, false)
}
// Remove removes the peer from the pool.
func (p *Pool) Remove(addr string) {
if addr == "" {
return
}
p.Lock()
defer p.Unlock()
idx := p.indexOfPeer(addr)
if idx >= 0 && idx < p.Size() {
if p.peers[idx].down {
p.downNum--
}
p.peers = append(p.peers[:idx], p.peers[idx+1:]...)
}
}
// Get return peer in smooth weighted roundrobin method.
func (p *Pool) Get(args ...interface{}) string {
p.RLock()
defer p.RUnlock()
var best *Peer
total := 0
for _, peer := range p.peers {
if peer.down {
continue
}
peer.Lock()
total += peer.effectiveWeight
peer.currentWeight += peer.effectiveWeight
if peer.effectiveWeight < peer.weight {
peer.effectiveWeight++
}
if best == nil || best.currentWeight < peer.currentWeight {
best = peer
}
peer.Unlock()
}
if best != nil {
best.Lock()
best.currentWeight -= total
best.Unlock()
return best.addr
}
return ""
}
// EqualGet get peer by turn, without considering weight.
func (p *Pool) EqualGet() string {
p.RLock()
defer p.RUnlock()
if p.Size() <= 0 {
return ""
}
if p.downNum >= p.Size() {
return ""
}
old := atomic.AddUint64(&p.current, 1) - 1
idx := old % uint64(p.Size())
peer := p.peers[idx]
if !peer.down {
return peer.addr
}
return p.EqualGet()
}
// CreatePool return a Pool object.
func CreatePool(pairs map[string]int) *Pool {
pool := &Pool{
current: 0,
downNum: 0,
}
for addr, weight := range pairs {
pool.Add(addr, weight)
}
return pool
}