hpds_control_center/internal/balance/roundRobinBalance.go

35 lines
655 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
2023-04-24 15:14:04 +08:00
rss []*model.NodeLastStateItem
2023-03-23 15:58:12 +08:00
}
func (r *RoundRobinBalance) Add(params model.NodeLastStateItem) error {
2023-04-24 15:14:04 +08:00
r.rss = append(r.rss, &params)
2023-03-23 15:58:12 +08:00
return nil
}
2023-04-24 15:14:04 +08:00
func (r *RoundRobinBalance) 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
}
lens := len(r.rss)
if r.curIndex >= lens {
r.curIndex = 0
}
curNode := r.rss[r.curIndex]
r.curIndex = (r.curIndex + 1) % lens
return curNode
}
2023-04-24 15:14:04 +08:00
func (r *RoundRobinBalance) Get(int64) (*model.NodeLastStateItem, error) {
2023-03-23 15:58:12 +08:00
return r.Next(), nil
}