hpds_control_center/internal/balance/roundRobinBalance.go

36 lines
620 B
Go
Raw Normal View History

2023-03-23 15:58:12 +08:00
package balance
import (
"hpds_control_center/model"
)
// RoundRobinBalance 轮询负载均衡
type RoundRobinBalance struct {
curIndex int
rss []int64
}
func (r *RoundRobinBalance) Add(params model.NodeLastStateItem) error {
nodeId := params.NodeId
r.rss = append(r.rss, nodeId)
return nil
}
func (r *RoundRobinBalance) Next() int64 {
if len(r.rss) == 0 {
return 0
}
lens := len(r.rss)
if r.curIndex >= lens {
r.curIndex = 0
}
curNode := r.rss[r.curIndex]
r.curIndex = (r.curIndex + 1) % lens
return curNode
}
func (r *RoundRobinBalance) Get(int64) (int64, error) {
return r.Next(), nil
}