This commit is contained in:
Lunny Xiao 2017-09-26 20:13:49 +08:00
parent b4db63b388
commit 02c48ef7ca
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 15 additions and 13 deletions

View File

@ -137,19 +137,21 @@ func (policy *WeightRoundRobinPolicy) Slave(g *EngineGroup) *Engine {
} }
// LeastConnPolicy implements GroupPolicy, every time will get the least connections slave // LeastConnPolicy implements GroupPolicy, every time will get the least connections slave
var LeastConnPolicy GroupPolicyHandler = func(g *EngineGroup) *Engine { func LeastConnPolicy() GroupPolicyHandler {
var slaves = g.Slaves() return func(g *EngineGroup) *Engine {
connections := 0 var slaves = g.Slaves()
idx := 0 connections := 0
for i, _ := range slaves { idx := 0
open_connections := slaves[i].DB().Stats().OpenConnections for i := 0; i < len(slaves); i++ {
if i == 0 { openConnections := slaves[i].DB().Stats().OpenConnections
connections = open_connections if i == 0 {
idx = i connections = openConnections
} else if open_connections <= connections { idx = i
connections = open_connections } else if openConnections <= connections {
idx = i connections = openConnections
idx = i
}
} }
return slaves[idx]
} }
return slaves[idx]
} }