DistributeX
57 строк · 1.1 Кб
1package scheduler
2
3import "DistributeX/internal/common"
4
5func WeightedRoundRobin(nodes []*common.ExecutionNode) func() *common.ExecutionNode {
6var currentIndex int
7var currentWeight int
8
9gcd := func(a, b int) int {
10for b != 0 {
11a, b = b, a%b
12}
13return a
14}
15
16gcdOfWeights := nodes[0].Weight
17for _, node := range nodes {
18gcdOfWeights = gcd(gcdOfWeights, node.Weight)
19}
20
21maxWeight := 0
22for _, node := range nodes {
23if node.Weight > maxWeight {
24maxWeight = node.Weight
25}
26}
27
28return func() *common.ExecutionNode {
29for {
30currentIndex = (currentIndex + 1) % len(nodes)
31if currentIndex == 0 {
32currentWeight = currentWeight - gcdOfWeights
33if currentWeight <= 0 {
34currentWeight = maxWeight
35}
36}
37
38if nodes[currentIndex].Weight >= currentWeight {
39return nodes[currentIndex]
40}
41}
42}
43}
44
45func LeastConnections(nodes []*common.ExecutionNode) *common.ExecutionNode {
46var selectedNode *common.ExecutionNode
47minConnections := int(^uint(0) >> 1)
48
49for _, node := range nodes {
50if node.Load < minConnections {
51selectedNode = node
52minConnections = node.Load
53}
54}
55
56return selectedNode
57}
58