simple code

This commit is contained in:
Lunny Xiao 2017-09-26 20:41:34 +08:00
parent 71beaa0190
commit 7b36611d51
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 44 additions and 85 deletions

View File

@ -21,7 +21,7 @@ func NewEngineGroup(args1 interface{}, args2 interface{}, policies ...GroupPolic
if len(policies) > 0 { if len(policies) > 0 {
eg.policy = policies[0] eg.policy = policies[0]
} else { } else {
eg.policy = NewRandomPolicy() eg.policy = RandomPolicy()
} }
driverName, ok1 := args1.(string) driverName, ok1 := args1.(string)

View File

@ -24,116 +24,75 @@ func (h GroupPolicyHandler) Slave(eg *EngineGroup) *Engine {
} }
// RandomPolicy implmentes randomly chose the slave of slaves // RandomPolicy implmentes randomly chose the slave of slaves
type RandomPolicy struct { func RandomPolicy() GroupPolicyHandler {
r *rand.Rand var r = rand.New(rand.NewSource(time.Now().UnixNano()))
} return func(g *EngineGroup) *Engine {
return g.Slaves()[r.Intn(len(g.Slaves()))]
// NewRandomPolicy creates a RandomPolicy
func NewRandomPolicy() *RandomPolicy {
return &RandomPolicy{
r: rand.New(rand.NewSource(time.Now().UnixNano())),
} }
} }
// Slave randomly choses the slave of slaves
func (policy *RandomPolicy) Slave(g *EngineGroup) *Engine {
return g.Slaves()[policy.r.Intn(len(g.Slaves()))]
}
// WeightRandomPolicy implmentes randomly chose the slave of slaves // WeightRandomPolicy implmentes randomly chose the slave of slaves
type WeightRandomPolicy struct { func WeightRandomPolicy(weights []int) GroupPolicyHandler {
weights []int
rands []int
r *rand.Rand
}
func NewWeightRandomPolicy(weights []int) *WeightRandomPolicy {
var rands = make([]int, 0, len(weights)) var rands = make([]int, 0, len(weights))
for i := 0; i < len(weights); i++ { for i := 0; i < len(weights); i++ {
for n := 0; n < weights[i]; n++ { for n := 0; n < weights[i]; n++ {
rands = append(rands, i) rands = append(rands, i)
} }
} }
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
return &WeightRandomPolicy{ return func(g *EngineGroup) *Engine {
weights: weights, var slaves = g.Slaves()
rands: rands, idx := rands[r.Intn(len(rands))]
r: rand.New(rand.NewSource(time.Now().UnixNano())), if idx >= len(slaves) {
idx = len(slaves) - 1
}
return slaves[idx]
} }
} }
func (policy *WeightRandomPolicy) Slave(g *EngineGroup) *Engine { func RoundRobinPolicy() GroupPolicyHandler {
var slaves = g.Slaves() var pos = -1
idx := policy.rands[policy.r.Intn(len(policy.rands))] var lock sync.Mutex
if idx >= len(slaves) { return func(g *EngineGroup) *Engine {
idx = len(slaves) - 1 var slaves = g.Slaves()
lock.Lock()
defer lock.Unlock()
pos++
if pos >= len(slaves) {
pos = 0
}
return slaves[pos]
} }
return slaves[idx]
} }
type RoundRobinPolicy struct { func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler {
pos int
lock sync.Mutex
}
func NewRoundRobinPolicy() *RoundRobinPolicy {
return &RoundRobinPolicy{pos: -1}
}
func (policy *RoundRobinPolicy) Slave(g *EngineGroup) *Engine {
var slaves = g.Slaves()
var pos int
policy.lock.Lock()
policy.pos++
if policy.pos >= len(slaves) {
policy.pos = 0
}
pos = policy.pos
policy.lock.Unlock()
return slaves[pos]
}
type WeightRoundRobinPolicy struct {
weights []int
rands []int
r *rand.Rand
lock sync.Mutex
pos int
}
func NewWeightRoundRobinPolicy(weights []int) *WeightRoundRobinPolicy {
var rands = make([]int, 0, len(weights)) var rands = make([]int, 0, len(weights))
for i := 0; i < len(weights); i++ { for i := 0; i < len(weights); i++ {
for n := 0; n < weights[i]; n++ { for n := 0; n < weights[i]; n++ {
rands = append(rands, i) rands = append(rands, i)
} }
} }
var pos = -1
var lock sync.Mutex
return &WeightRoundRobinPolicy{ return func(g *EngineGroup) *Engine {
weights: weights, var slaves = g.Slaves()
rands: rands, lock.Lock()
r: rand.New(rand.NewSource(time.Now().UnixNano())), defer lock.Unlock()
pos: -1, pos++
} if pos >= len(rands) {
} pos = 0
}
func (policy *WeightRoundRobinPolicy) Slave(g *EngineGroup) *Engine { idx := rands[pos]
var slaves = g.Slaves() if idx >= len(slaves) {
var pos int idx = len(slaves) - 1
policy.lock.Lock() }
policy.pos++ return slaves[idx]
if policy.pos >= len(policy.rands) {
policy.pos = 0
} }
pos = policy.pos
policy.lock.Unlock()
idx := policy.rands[pos]
if idx >= len(slaves) {
idx = len(slaves) - 1
}
return slaves[idx]
} }
// LeastConnPolicy implements GroupPolicy, every time will get the least connections slave // LeastConnPolicy implements GroupPolicy, every time will get the least connections slave