rename and bug fix for WeightRoundRobinPolicy

This commit is contained in:
Lunny Xiao 2017-09-26 11:36:12 +08:00
parent 8353ce81e9
commit 3f4ec27ef7
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 5 additions and 5 deletions

View File

@ -80,7 +80,7 @@ func (policy *RoundRobinPolicy) Slave(g *EngineGroup) *Engine {
return g.Slaves()[pos]
}
type WeightRoundRobin struct {
type WeightRoundRobinPolicy struct {
weights []int
rands []int
r *rand.Rand
@ -88,7 +88,7 @@ type WeightRoundRobin struct {
pos int
}
func NewWeightRoundRobin(weights []int) *WeightRoundRobin {
func NewWeightRoundRobinPolicy(weights []int) *WeightRoundRobinPolicy {
var rands = make([]int, 0, len(weights))
for i := 0; i < len(weights); i++ {
for n := 0; n < weights[i]; n++ {
@ -96,7 +96,7 @@ func NewWeightRoundRobin(weights []int) *WeightRoundRobin {
}
}
return &WeightRoundRobin{
return &WeightRoundRobinPolicy{
weights: weights,
rands: rands,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
@ -104,12 +104,12 @@ func NewWeightRoundRobin(weights []int) *WeightRoundRobin {
}
}
func (policy *WeightRoundRobin) Slave(g *EngineGroup) *Engine {
func (policy *WeightRoundRobinPolicy) Slave(g *EngineGroup) *Engine {
var slaves = g.Slaves()
var pos int
policy.lock.Lock()
policy.pos++
if policy.pos >= len(g.Slaves()) {
if policy.pos >= len(policy.rands) {
policy.pos = 0
}
pos = policy.pos