hpds_control_center/internal/balance/roundRobinBalance.go

36 lines
620 B
Go

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
}