2020-09-15 14:44:38 +08:00
|
|
|
package schedulingconfigs
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
"hash/crc32"
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
// HashScheduling Hash调度算法
|
2020-09-13 19:27:47 +08:00
|
|
|
type HashScheduling struct {
|
|
|
|
|
Scheduling
|
|
|
|
|
|
|
|
|
|
count uint32
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
// Start 启动
|
2020-09-13 19:27:47 +08:00
|
|
|
func (this *HashScheduling) Start() {
|
|
|
|
|
this.count = uint32(len(this.Candidates))
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
// Next 获取下一个候选对象
|
2020-09-13 19:27:47 +08:00
|
|
|
func (this *HashScheduling) Next(call *shared.RequestCall) CandidateInterface {
|
|
|
|
|
if this.count == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := call.Options.GetString("key")
|
|
|
|
|
|
|
|
|
|
if call.Formatter != nil {
|
|
|
|
|
key = call.Formatter(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sum := crc32.ChecksumIEEE([]byte(key))
|
|
|
|
|
return this.Candidates[sum%this.count]
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
// Summary 获取简要信息
|
2020-09-13 19:27:47 +08:00
|
|
|
func (this *HashScheduling) Summary() maps.Map {
|
|
|
|
|
return maps.Map{
|
|
|
|
|
"code": "hash",
|
|
|
|
|
"name": "Hash算法",
|
2020-09-15 14:44:38 +08:00
|
|
|
"description": "根据自定义的键值的Hash值分配源站",
|
2020-09-13 19:27:47 +08:00
|
|
|
"networks": []string{"http"},
|
|
|
|
|
}
|
|
|
|
|
}
|