From 52a84b29b7e97dcda44aef768fc289a81530c76a Mon Sep 17 00:00:00 2001 From: WhiteBatman Date: Tue, 26 Sep 2017 12:07:49 +0800 Subject: [PATCH 1/2] 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++ From d8947626190d9abb8d3aadb39cf30a48fc460a33 Mon Sep 17 00:00:00 2001 From: WhiteBatman Date: Tue, 26 Sep 2017 12:32:21 +0800 Subject: [PATCH 2/2] modify Slave function and add LeastConnPolicy --- engine_group.go | 3 +++ engine_group_policy.go | 30 ++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/engine_group.go b/engine_group.go index 88d0db0f..093d48aa 100644 --- a/engine_group.go +++ b/engine_group.go @@ -91,6 +91,9 @@ func (eg *EngineGroup) Slave() *Engine { if eg.count == 1 { return eg.master } + if eg.s_count == 1 { + return eg.slaves[0] + } return eg.policy.Slave(eg) } diff --git a/engine_group_policy.go b/engine_group_policy.go index e05663bc..c4fdcb0d 100644 --- a/engine_group_policy.go +++ b/engine_group_policy.go @@ -25,9 +25,6 @@ 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()))] } @@ -54,9 +51,6 @@ 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 @@ -74,10 +68,6 @@ 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++ @@ -116,10 +106,6 @@ 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++ @@ -144,6 +130,18 @@ func NewLeastConnPolicy() *LeastConnPolicy { } func (policy *LeastConnPolicy) Slave(g *EngineGroup) *Engine { - panic("not implementation") - return nil + var slaves = g.Slaves() + connections := 0 + idx := 0 + for i, _ := range slaves { + open_connections := slaves[i].Stats() + if i == 0 { + connections = open_connections + idx = i + } else if open_connections <= connections { + connections = open_connections + idx = i + } + } + return slaves[idx] }