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"
|
|
|
|
|
|
"math"
|
|
|
|
|
|
"math/rand"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
|
// RandomScheduling 随机调度算法
|
2020-09-13 19:27:47 +08:00
|
|
|
|
type RandomScheduling struct {
|
|
|
|
|
|
Scheduling
|
|
|
|
|
|
|
|
|
|
|
|
array []CandidateInterface
|
|
|
|
|
|
count uint // 实际总的服务器数
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
|
// Start 启动
|
2020-09-13 19:27:47 +08:00
|
|
|
|
func (this *RandomScheduling) Start() {
|
|
|
|
|
|
sumWeight := uint(0)
|
|
|
|
|
|
for _, c := range this.Candidates {
|
|
|
|
|
|
weight := c.CandidateWeight()
|
|
|
|
|
|
if weight == 0 {
|
|
|
|
|
|
weight = 1
|
|
|
|
|
|
} else if weight > 10000 {
|
|
|
|
|
|
weight = 10000
|
|
|
|
|
|
}
|
|
|
|
|
|
sumWeight += weight
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if sumWeight == 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, c := range this.Candidates {
|
|
|
|
|
|
weight := c.CandidateWeight()
|
|
|
|
|
|
if weight == 0 {
|
|
|
|
|
|
weight = 1
|
|
|
|
|
|
} else if weight > 10000 {
|
|
|
|
|
|
weight = 10000
|
|
|
|
|
|
}
|
|
|
|
|
|
count := uint(0)
|
|
|
|
|
|
if sumWeight <= 1000 {
|
|
|
|
|
|
count = weight
|
|
|
|
|
|
} else {
|
|
|
|
|
|
count = uint(math.Round(float64(weight*10000) / float64(sumWeight))) // 1% 产生 100个数据,最多支持10000个服务器
|
|
|
|
|
|
}
|
|
|
|
|
|
for i := uint(0); i < count; i++ {
|
|
|
|
|
|
this.array = append(this.array, c)
|
|
|
|
|
|
}
|
|
|
|
|
|
this.count += count
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
|
// Next 获取下一个候选对象
|
2020-09-13 19:27:47 +08:00
|
|
|
|
func (this *RandomScheduling) Next(call *shared.RequestCall) CandidateInterface {
|
|
|
|
|
|
if this.count == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if this.count == 1 {
|
|
|
|
|
|
return this.array[0]
|
|
|
|
|
|
}
|
|
|
|
|
|
index := rand.Int() % int(this.count)
|
|
|
|
|
|
return this.array[index]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
|
// Summary 获取简要信息
|
2020-09-13 19:27:47 +08:00
|
|
|
|
func (this *RandomScheduling) Summary() maps.Map {
|
|
|
|
|
|
return maps.Map{
|
|
|
|
|
|
"code": "random",
|
|
|
|
|
|
"name": "Random随机算法",
|
2020-09-15 14:44:38 +08:00
|
|
|
|
"description": "根据权重设置随机分配源站",
|
2021-09-22 19:39:55 +08:00
|
|
|
|
"networks": []string{"http", "tcp", "udp", "unix"},
|
2020-09-13 19:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|