modify policy interface

This commit is contained in:
WhiteBatman 2017-09-26 10:22:13 +08:00 committed by Lunny Xiao
parent 3bbbd9d215
commit c618aab9e1
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 13 additions and 9 deletions

View File

@ -62,7 +62,7 @@ func newGroup1(driverName string, dataSourceNames string, policy ...Policy) (*En
s_count: len(engines[1:]),
policy: policy[0],
}
eg.policy.SetEngineGroup(eg)
eg.policy.Init()
return eg, nil
} else {
xPolicy := new(XormEngineGroupPolicy)
@ -73,7 +73,7 @@ func newGroup1(driverName string, dataSourceNames string, policy ...Policy) (*En
s_count: len(engines[1:]),
policy: xPolicy,
}
xPolicy.SetEngineGroup(eg)
xPolicy.Init()
return eg, nil
}
@ -91,7 +91,7 @@ func newGroup2(Master *Engine, Slaves []*Engine, policy ...Policy) (*EngineGroup
s_count: len(Slaves),
policy: policy[0],
}
eg.policy.SetEngineGroup(eg)
eg.policy.Init()
return eg, nil
} else {
xPolicy := new(XormEngineGroupPolicy)
@ -102,7 +102,7 @@ func newGroup2(Master *Engine, Slaves []*Engine, policy ...Policy) (*EngineGroup
s_count: len(Slaves),
policy: xPolicy,
}
xPolicy.SetEngineGroup(eg)
xPolicy.Init()
return eg, nil
}
}
@ -149,7 +149,7 @@ func (eg *EngineGroup) Slave() *Engine {
if eg.count == 1 {
return eg.master
}
return eg.slaves[eg.policy.Slave()]
return eg.policy.Slave(eg)
}
func (eg *EngineGroup) Slaves() []*Engine {

View File

@ -19,8 +19,8 @@ const (
)
type Policy interface {
Slave() int
SetEngineGroup(*EngineGroup)
Init()
Slave(*EngineGroup) *Engine
}
type XormEngineGroupPolicy struct {
@ -30,9 +30,13 @@ type XormEngineGroupPolicy struct {
r *rand.Rand
}
func (xgep *XormEngineGroupPolicy) SetEngineGroup(eg *EngineGroup) {
func (xgep *XormEngineGroupPolicy) Init() {
xgep.r = rand.New(rand.NewSource(time.Now().UnixNano()))
}
func (xgep *XormEngineGroupPolicy) Slave(eg *EngineGroup) *Engine {
xgep.eg = eg
return eg.slaves[xgep.slave()]
}
func (xgep *XormEngineGroupPolicy) SetWeight() {
@ -44,7 +48,7 @@ func (xgep *XormEngineGroupPolicy) SetWeight() {
}
}
func (xgep *XormEngineGroupPolicy) Slave() int {
func (xgep *XormEngineGroupPolicy) slave() int {
switch xgep.eg.p {
case ENGINE_GROUP_POLICY_RANDOM:
return xgep.Random()