rename and bug fix for WeightRoundRobinPolicy

This commit is contained in:
Lunny Xiao 2017-09-26 11:36:12 +08:00
parent 2e5accf6f1
commit f25eb5254b
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] return g.Slaves()[pos]
} }
type WeightRoundRobin struct { type WeightRoundRobinPolicy struct {
weights []int weights []int
rands []int rands []int
r *rand.Rand r *rand.Rand
@ -88,7 +88,7 @@ type WeightRoundRobin struct {
pos int pos int
} }
func NewWeightRoundRobin(weights []int) *WeightRoundRobin { func NewWeightRoundRobinPolicy(weights []int) *WeightRoundRobinPolicy {
var rands = make([]int, 0, len(weights)) var rands = make([]int, 0, len(weights))
for i := 0; i < len(weights); i++ { for i := 0; i < len(weights); i++ {
for n := 0; n < weights[i]; n++ { for n := 0; n < weights[i]; n++ {
@ -96,7 +96,7 @@ func NewWeightRoundRobin(weights []int) *WeightRoundRobin {
} }
} }
return &WeightRoundRobin{ return &WeightRoundRobinPolicy{
weights: weights, weights: weights,
rands: rands, rands: rands,
r: rand.New(rand.NewSource(time.Now().UnixNano())), 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 slaves = g.Slaves()
var pos int var pos int
policy.lock.Lock() policy.lock.Lock()
policy.pos++ policy.pos++
if policy.pos >= len(g.Slaves()) { if policy.pos >= len(policy.rands) {
policy.pos = 0 policy.pos = 0
} }
pos = policy.pos pos = policy.pos