add some comments and refactor
This commit is contained in:
parent
741af4a315
commit
8f73a779a8
|
@ -10,24 +10,32 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GroupPolicy is be used by chosing the current slave from slaves
|
||||||
type GroupPolicy interface {
|
type GroupPolicy interface {
|
||||||
Slave(*EngineGroup) *Engine
|
Slave(*EngineGroup) *Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GroupPolicyHandler should be used when a function is a GroupPolicy
|
||||||
|
type GroupPolicyHandler func(*EngineGroup) *Engine
|
||||||
|
|
||||||
|
// RandomPolicy implmentes randomly chose the slave of slaves
|
||||||
type RandomPolicy struct {
|
type RandomPolicy struct {
|
||||||
r *rand.Rand
|
r *rand.Rand
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewRandomPolicy creates a RandomPolicy
|
||||||
func NewRandomPolicy() *RandomPolicy {
|
func NewRandomPolicy() *RandomPolicy {
|
||||||
return &RandomPolicy{
|
return &RandomPolicy{
|
||||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Slave randomly choses the slave of slaves
|
||||||
func (policy *RandomPolicy) Slave(g *EngineGroup) *Engine {
|
func (policy *RandomPolicy) Slave(g *EngineGroup) *Engine {
|
||||||
return g.Slaves()[policy.r.Intn(len(g.Slaves()))]
|
return g.Slaves()[policy.r.Intn(len(g.Slaves()))]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WeightRandomPolicy implmentes randomly chose the slave of slaves
|
||||||
type WeightRandomPolicy struct {
|
type WeightRandomPolicy struct {
|
||||||
weights []int
|
weights []int
|
||||||
rands []int
|
rands []int
|
||||||
|
@ -123,14 +131,8 @@ func (policy *WeightRoundRobinPolicy) Slave(g *EngineGroup) *Engine {
|
||||||
return slaves[idx]
|
return slaves[idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
type LeastConnPolicy struct {
|
// LeastConnPolicy implements GroupPolicy, every time will get the least connections slave
|
||||||
}
|
var LeastConnPolicy GroupPolicyHandler = func(g *EngineGroup) *Engine {
|
||||||
|
|
||||||
func NewLeastConnPolicy() *LeastConnPolicy {
|
|
||||||
return &LeastConnPolicy{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (policy *LeastConnPolicy) Slave(g *EngineGroup) *Engine {
|
|
||||||
var slaves = g.Slaves()
|
var slaves = g.Slaves()
|
||||||
connections := 0
|
connections := 0
|
||||||
idx := 0
|
idx := 0
|
||||||
|
|
Loading…
Reference in New Issue