-
Notifications
You must be signed in to change notification settings - Fork 1
/
roundrobin.go
56 lines (46 loc) · 993 Bytes
/
roundrobin.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
// SPDX-FileCopyrightText: 2018-2024 caixw
//
// SPDX-License-Identifier: MIT
package selector
import "sync"
type roundRobin struct {
curr int
peers []Peer
mux sync.RWMutex
}
// NewRoundRobin 轮询法
//
// weight 是否采用加权算法,如果此值为 true,
// 在调用 [Selector.Add] 时参数必须实现 [WeightedPeer]。
func NewRoundRobin(weight bool, cap int) Updateable {
if weight {
return newWeightedRoundRobin(cap)
}
return &roundRobin{
peers: make([]Peer, 0, cap),
}
}
func (s *roundRobin) Next() (string, error) {
s.mux.RLock()
defer s.mux.RUnlock()
l := len(s.peers)
switch l {
case 0:
return "", ErrNoPeer()
case 1:
return s.peers[0].Addr(), nil
default:
if s.curr++; s.curr >= l {
s.curr = 0
}
return s.peers[s.curr].Addr(), nil
}
}
func (s *roundRobin) Update(peers ...Peer) {
s.mux.Lock()
s.peers = append(s.peers[:0], peers...)
if s.curr >= len(s.peers) { // 判断 curr 是否超了
s.curr = 0
}
s.mux.Unlock()
}