-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
node_selector_round_robin.go
50 lines (41 loc) · 1.09 KB
/
node_selector_round_robin.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
package client
import (
"sync/atomic"
"github.com/smartcontractkit/chainlink/v2/common/types"
)
type roundRobinSelector[
CHAIN_ID types.ID,
HEAD Head,
RPC NodeClient[CHAIN_ID, HEAD],
] struct {
nodes []Node[CHAIN_ID, HEAD, RPC]
roundRobinCount atomic.Uint32
}
func NewRoundRobinSelector[
CHAIN_ID types.ID,
HEAD Head,
RPC NodeClient[CHAIN_ID, HEAD],
](nodes []Node[CHAIN_ID, HEAD, RPC]) NodeSelector[CHAIN_ID, HEAD, RPC] {
return &roundRobinSelector[CHAIN_ID, HEAD, RPC]{
nodes: nodes,
}
}
func (s *roundRobinSelector[CHAIN_ID, HEAD, RPC]) Select() Node[CHAIN_ID, HEAD, RPC] {
var liveNodes []Node[CHAIN_ID, HEAD, RPC]
for _, n := range s.nodes {
if n.State() == nodeStateAlive {
liveNodes = append(liveNodes, n)
}
}
nNodes := len(liveNodes)
if nNodes == 0 {
return nil
}
// NOTE: Inc returns the number after addition, so we must -1 to get the "current" counter
count := s.roundRobinCount.Add(1) - 1
idx := int(count % uint32(nNodes))
return liveNodes[idx]
}
func (s *roundRobinSelector[CHAIN_ID, HEAD, RPC]) Name() string {
return NodeSelectionModeRoundRobin
}