From 02c48ef7caf30e8955edfac14d25d6a9d20bba9b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 26 Sep 2017 20:13:49 +0800 Subject: [PATCH] refactor --- engine_group_policy.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/engine_group_policy.go b/engine_group_policy.go index e79f7d52..7db41c20 100644 --- a/engine_group_policy.go +++ b/engine_group_policy.go @@ -137,19 +137,21 @@ func (policy *WeightRoundRobinPolicy) Slave(g *EngineGroup) *Engine { } // LeastConnPolicy implements GroupPolicy, every time will get the least connections slave -var LeastConnPolicy GroupPolicyHandler = func(g *EngineGroup) *Engine { - var slaves = g.Slaves() - connections := 0 - idx := 0 - for i, _ := range slaves { - open_connections := slaves[i].DB().Stats().OpenConnections - if i == 0 { - connections = open_connections - idx = i - } else if open_connections <= connections { - connections = open_connections - idx = i +func LeastConnPolicy() GroupPolicyHandler { + return func(g *EngineGroup) *Engine { + var slaves = g.Slaves() + connections := 0 + idx := 0 + for i := 0; i < len(slaves); i++ { + openConnections := slaves[i].DB().Stats().OpenConnections + if i == 0 { + connections = openConnections + idx = i + } else if openConnections <= connections { + connections = openConnections + idx = i + } } + return slaves[idx] } - return slaves[idx] }