From 2b99f137a1e5f943ce918a9293ba5ecaa723e544 Mon Sep 17 00:00:00 2001 From: WhiteBatman Date: Tue, 26 Sep 2017 12:07:49 +0800 Subject: [PATCH] modify Slave function --- engine_group_policy.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/engine_group_policy.go b/engine_group_policy.go index 88412eaf..e05663bc 100644 --- a/engine_group_policy.go +++ b/engine_group_policy.go @@ -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++