DistributeX

Форк
0
57 строк · 1.1 Кб
1
package scheduler
2

3
import "DistributeX/internal/common"
4

5
func WeightedRoundRobin(nodes []*common.ExecutionNode) func() *common.ExecutionNode {
6
	var currentIndex int
7
	var currentWeight int
8

9
	gcd := func(a, b int) int {
10
		for b != 0 {
11
			a, b = b, a%b
12
		}
13
		return a
14
	}
15

16
	gcdOfWeights := nodes[0].Weight
17
	for _, node := range nodes {
18
		gcdOfWeights = gcd(gcdOfWeights, node.Weight)
19
	}
20

21
	maxWeight := 0
22
	for _, node := range nodes {
23
		if node.Weight > maxWeight {
24
			maxWeight = node.Weight
25
		}
26
	}
27

28
	return func() *common.ExecutionNode {
29
		for {
30
			currentIndex = (currentIndex + 1) % len(nodes)
31
			if currentIndex == 0 {
32
				currentWeight = currentWeight - gcdOfWeights
33
				if currentWeight <= 0 {
34
					currentWeight = maxWeight
35
				}
36
			}
37

38
			if nodes[currentIndex].Weight >= currentWeight {
39
				return nodes[currentIndex]
40
			}
41
		}
42
	}
43
}
44

45
func LeastConnections(nodes []*common.ExecutionNode) *common.ExecutionNode {
46
	var selectedNode *common.ExecutionNode
47
	minConnections := int(^uint(0) >> 1)
48

49
	for _, node := range nodes {
50
		if node.Load < minConnections {
51
			selectedNode = node
52
			minConnections = node.Load
53
		}
54
	}
55

56
	return selectedNode
57
}
58

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.