diff --git a/engine_group.go b/engine_group.go index dd1e6380..8b8118be 100644 --- a/engine_group.go +++ b/engine_group.go @@ -73,6 +73,9 @@ func (eg *EngineGroup) Slave() *Engine { case 1: return eg.slaves[0] } + 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 88412eaf..c4fdcb0d 100644 --- a/engine_group_policy.go +++ b/engine_group_policy.go @@ -71,7 +71,7 @@ func (policy *RoundRobinPolicy) Slave(g *EngineGroup) *Engine { 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 @@ -130,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] }