33 lines
522 B
Go
33 lines
522 B
Go
|
package balance
|
||
|
|
||
|
import (
|
||
|
"hpds_control_center/model"
|
||
|
"math/rand"
|
||
|
)
|
||
|
|
||
|
// RandomBalance 随机负载均衡
|
||
|
type RandomBalance struct {
|
||
|
curIndex int
|
||
|
|
||
|
rss []int64
|
||
|
}
|
||
|
|
||
|
func (r *RandomBalance) Add(params model.NodeLastStateItem) error {
|
||
|
nodeId := params.NodeId
|
||
|
r.rss = append(r.rss, nodeId)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (r *RandomBalance) Next() int64 {
|
||
|
if len(r.rss) == 0 {
|
||
|
return 0
|
||
|
}
|
||
|
r.curIndex = rand.Intn(len(r.rss))
|
||
|
return r.rss[r.curIndex]
|
||
|
}
|
||
|
|
||
|
func (r *RandomBalance) Get(int64) (int64, error) {
|
||
|
return r.Next(), nil
|
||
|
}
|