-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduling_hash.go
50 lines (40 loc) · 1.04 KB
/
scheduling_hash.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 schedulingconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
"hash/crc32"
)
// HashScheduling Hash调度算法
type HashScheduling struct {
Scheduling
count int
}
// Start 启动
func (this *HashScheduling) Start() {
this.count = len(this.Candidates)
}
// Next 获取下一个候选对象
func (this *HashScheduling) Next(call *shared.RequestCall) CandidateInterface {
if this.count == 0 {
return nil
}
if call == nil || call.Options == nil {
return this.Candidates[rands.Int(0, this.count-1)]
}
key := call.Options.GetString("key")
if call.Formatter != nil {
key = call.Formatter(key)
}
sum := crc32.ChecksumIEEE([]byte(key))
return this.Candidates[sum%uint32(this.count)]
}
// Summary 获取简要信息
func (this *HashScheduling) Summary() maps.Map {
return maps.Map{
"code": "hash",
"name": "Hash算法",
"description": "根据自定义的键值的Hash值分配源站",
"networks": []string{"http"},
}
}