2023-03-23 15:58:12 +08:00
|
|
|
package balance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hpds_control_center/model"
|
|
|
|
"math/rand"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RandomBalance 随机负载均衡
|
|
|
|
type RandomBalance struct {
|
|
|
|
curIndex int
|
|
|
|
|
2023-04-24 15:14:04 +08:00
|
|
|
rss []*model.NodeLastStateItem
|
2023-03-23 15:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RandomBalance) Add(params model.NodeLastStateItem) error {
|
2023-04-24 15:14:04 +08:00
|
|
|
r.rss = append(r.rss, ¶ms)
|
2023-03-23 15:58:12 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:14:04 +08:00
|
|
|
func (r *RandomBalance) Next() *model.NodeLastStateItem {
|
2023-03-23 15:58:12 +08:00
|
|
|
if len(r.rss) == 0 {
|
2023-04-24 15:14:04 +08:00
|
|
|
return nil
|
2023-03-23 15:58:12 +08:00
|
|
|
}
|
|
|
|
r.curIndex = rand.Intn(len(r.rss))
|
|
|
|
return r.rss[r.curIndex]
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:14:04 +08:00
|
|
|
func (r *RandomBalance) Get(int64) (*model.NodeLastStateItem, error) {
|
2023-03-23 15:58:12 +08:00
|
|
|
return r.Next(), nil
|
|
|
|
}
|