-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_selector.go
More file actions
44 lines (40 loc) · 1.26 KB
/
node_selector.go
File metadata and controls
44 lines (40 loc) · 1.26 KB
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
package multinode
import (
"fmt"
)
const (
NodeSelectionModeHighestHead = "HighestHead"
NodeSelectionModeRoundRobin = "RoundRobin"
NodeSelectionModeTotalDifficulty = "TotalDifficulty"
NodeSelectionModePriorityLevel = "PriorityLevel"
NodeSelectionModeRandomRPC = "RandomRPC"
)
type NodeSelector[
CHAIN_ID ID,
RPC any,
] interface {
// Select returns a Node, or nil if none can be selected.
// Implementation must be thread-safe.
Select() Node[CHAIN_ID, RPC]
// Name returns the strategy name, e.g. "HighestHead" or "RoundRobin"
Name() string
}
func newNodeSelector[
CHAIN_ID ID,
RPC any,
](selectionMode string, nodes []Node[CHAIN_ID, RPC]) NodeSelector[CHAIN_ID, RPC] {
switch selectionMode {
case NodeSelectionModeHighestHead:
return NewHighestHeadNodeSelector[CHAIN_ID, RPC](nodes)
case NodeSelectionModeRoundRobin:
return NewRoundRobinSelector[CHAIN_ID, RPC](nodes)
case NodeSelectionModeTotalDifficulty:
return NewTotalDifficultyNodeSelector[CHAIN_ID, RPC](nodes)
case NodeSelectionModePriorityLevel:
return NewPriorityLevelNodeSelector[CHAIN_ID, RPC](nodes)
case NodeSelectionModeRandomRPC:
return NewRandomRPCSelector[CHAIN_ID, RPC](nodes)
default:
panic(fmt.Sprintf("unsupported NodeSelectionMode: %s", selectionMode))
}
}