rename and bug fix for WeightRoundRobinPolicy
This commit is contained in:
parent
2e5accf6f1
commit
f25eb5254b
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue