modify Slave function

This commit is contained in:
WhiteBatman 2017-09-26 12:07:49 +08:00 committed by Lunny Xiao
parent f25eb5254b
commit 2b99f137a1
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 15 additions and 1 deletions

View File

@ -25,6 +25,9 @@ func NewRandomPolicy() *RandomPolicy {
}
func (policy *RandomPolicy) Slave(g *EngineGroup) *Engine {
if g.s_count == 1 {
return g.Slaves()[0]
}
return g.Slaves()[policy.r.Intn(len(g.Slaves()))]
}
@ -51,6 +54,9 @@ func NewWeightRandomPolicy(weights []int) *WeightRandomPolicy {
func (policy *WeightRandomPolicy) Slave(g *EngineGroup) *Engine {
var slaves = g.Slaves()
if g.s_count == 1 {
return slaves[0]
}
idx := policy.rands[policy.r.Intn(len(policy.rands))]
if idx >= len(slaves) {
idx = len(slaves) - 1
@ -68,10 +74,14 @@ func NewRoundRobinPolicy() *RoundRobinPolicy {
}
func (policy *RoundRobinPolicy) Slave(g *EngineGroup) *Engine {
if g.s_count == 1 {
return g.Slaves()[0]
}
var pos int
policy.lock.Lock()
policy.pos++
if policy.pos >= len(g.Slaves()) {
if policy.pos >= g.s_count {
policy.pos = 0
}
pos = policy.pos
@ -106,6 +116,10 @@ func NewWeightRoundRobinPolicy(weights []int) *WeightRoundRobinPolicy {
func (policy *WeightRoundRobinPolicy) Slave(g *EngineGroup) *Engine {
var slaves = g.Slaves()
if g.s_count == 1 {
return slaves[0]
}
var pos int
policy.lock.Lock()
policy.pos++