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 {
return func(g *EngineGroup) *Engine {
var slaves = g.Slaves() var slaves = g.Slaves()
connections := 0 connections := 0
idx := 0 idx := 0
for i, _ := range slaves { for i := 0; i < len(slaves); i++ {
open_connections := slaves[i].DB().Stats().OpenConnections openConnections := slaves[i].DB().Stats().OpenConnections
if i == 0 { if i == 0 {
connections = open_connections connections = openConnections
idx = i idx = i
} else if open_connections <= connections { } else if openConnections <= connections {
connections = open_connections connections = openConnections
idx = i idx = i
} }
} }
return slaves[idx] return slaves[idx]
}
} }