Merge branch 'group-engine' of https://github.com/WhiteBatman/xorm into WhiteBatman/group-engine

This commit is contained in:
Lunny Xiao 2017-09-26 12:51:10 +08:00
commit 3981aa9b1a
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 18 additions and 3 deletions

View File

@ -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)
}

View File

@ -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]
}