mirror of
https://github.com/razertory/Ginx.git
synced 2020-01-07 15:43:17 +00:00
fmt
This commit is contained in:
+124
-124
@@ -2,194 +2,194 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
type RR interface {
|
type RR interface {
|
||||||
Next() interface{}
|
Next() interface{}
|
||||||
Add(node interface{}, weight int)
|
Add(node interface{}, weight int)
|
||||||
RemoveAll()
|
RemoveAll()
|
||||||
Reset()
|
Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RR_NGINX = 0 //Nginx算法
|
RR_NGINX = 0 //Nginx算法
|
||||||
RR_LVS = 1 //LVS算法
|
RR_LVS = 1 //LVS算法
|
||||||
)
|
)
|
||||||
|
|
||||||
//算法实现工厂类
|
//算法实现工厂类
|
||||||
func NewWeightedRR(rtype int) RR {
|
func NewWeightedRR(rtype int) RR {
|
||||||
if rtype == RR_NGINX {
|
if rtype == RR_NGINX {
|
||||||
return &WNGINX{}
|
return &WNGINX{}
|
||||||
} else if rtype == RR_LVS {
|
} else if rtype == RR_LVS {
|
||||||
return &WLVS{}
|
return &WLVS{}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//节点结构
|
//节点结构
|
||||||
type WeightNginx struct {
|
type WeightNginx struct {
|
||||||
Node interface{}
|
Node interface{}
|
||||||
Weight int
|
Weight int
|
||||||
CurrentWeight int
|
CurrentWeight int
|
||||||
EffectiveWeight int
|
EffectiveWeight int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ww *WeightNginx) fail() {
|
func (ww *WeightNginx) fail() {
|
||||||
ww.EffectiveWeight -= ww.Weight
|
ww.EffectiveWeight -= ww.Weight
|
||||||
if ww.EffectiveWeight < 0 {
|
if ww.EffectiveWeight < 0 {
|
||||||
ww.EffectiveWeight = 0
|
ww.EffectiveWeight = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//nginx算法实现类
|
//nginx算法实现类
|
||||||
type WNGINX struct {
|
type WNGINX struct {
|
||||||
nodes []*WeightNginx
|
nodes []*WeightNginx
|
||||||
n int
|
n int
|
||||||
}
|
}
|
||||||
|
|
||||||
//增加权重节点
|
//增加权重节点
|
||||||
func (w *WNGINX) Add(node interface{}, weight int) {
|
func (w *WNGINX) Add(node interface{}, weight int) {
|
||||||
weighted := &WeightNginx{
|
weighted := &WeightNginx{
|
||||||
Node: node,
|
Node: node,
|
||||||
Weight: weight,
|
Weight: weight,
|
||||||
EffectiveWeight: weight}
|
EffectiveWeight: weight}
|
||||||
w.nodes = append(w.nodes, weighted)
|
w.nodes = append(w.nodes, weighted)
|
||||||
w.n++
|
w.n++
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WNGINX) RemoveAll() {
|
func (w *WNGINX) RemoveAll() {
|
||||||
w.nodes = w.nodes[:0]
|
w.nodes = w.nodes[:0]
|
||||||
w.n = 0
|
w.n = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
//下次轮询事件
|
//下次轮询事件
|
||||||
func (w *WNGINX) Next() interface{} {
|
func (w *WNGINX) Next() interface{} {
|
||||||
if w.n == 0 {
|
if w.n == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if w.n == 1 {
|
if w.n == 1 {
|
||||||
return w.nodes[0].Node
|
return w.nodes[0].Node
|
||||||
}
|
}
|
||||||
|
|
||||||
return nextWeightedNode(w.nodes).Node
|
return nextWeightedNode(w.nodes).Node
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextWeightedNode(nodes []*WeightNginx) (best *WeightNginx) {
|
func nextWeightedNode(nodes []*WeightNginx) (best *WeightNginx) {
|
||||||
total := 0
|
total := 0
|
||||||
|
|
||||||
for i := 0; i < len(nodes); i++ {
|
for i := 0; i < len(nodes); i++ {
|
||||||
w := nodes[i]
|
w := nodes[i]
|
||||||
|
|
||||||
if w == nil {
|
if w == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
w.CurrentWeight += w.EffectiveWeight
|
w.CurrentWeight += w.EffectiveWeight
|
||||||
total += w.EffectiveWeight
|
total += w.EffectiveWeight
|
||||||
if w.EffectiveWeight < w.Weight {
|
if w.EffectiveWeight < w.Weight {
|
||||||
w.EffectiveWeight++
|
w.EffectiveWeight++
|
||||||
}
|
}
|
||||||
|
|
||||||
if best == nil || w.CurrentWeight > best.CurrentWeight {
|
if best == nil || w.CurrentWeight > best.CurrentWeight {
|
||||||
best = w
|
best = w
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if best == nil {
|
if best == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
best.CurrentWeight -= total
|
best.CurrentWeight -= total
|
||||||
return best
|
return best
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WNGINX) Reset() {
|
func (w *WNGINX) Reset() {
|
||||||
for _, s := range w.nodes {
|
for _, s := range w.nodes {
|
||||||
s.EffectiveWeight = s.Weight
|
s.EffectiveWeight = s.Weight
|
||||||
s.CurrentWeight = 0
|
s.CurrentWeight = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//节点结构
|
//节点结构
|
||||||
type WeightLvs struct {
|
type WeightLvs struct {
|
||||||
Node interface{}
|
Node interface{}
|
||||||
Weight int
|
Weight int
|
||||||
}
|
}
|
||||||
|
|
||||||
//lvs算法实现类
|
//lvs算法实现类
|
||||||
type WLVS struct {
|
type WLVS struct {
|
||||||
nodes []*WeightLvs
|
nodes []*WeightLvs
|
||||||
n int
|
n int
|
||||||
gcd int //通用的权重因子
|
gcd int //通用的权重因子
|
||||||
maxW int //最大权重
|
maxW int //最大权重
|
||||||
i int //被选择的次数
|
i int //被选择的次数
|
||||||
cw int //当前的权重值
|
cw int //当前的权重值
|
||||||
}
|
}
|
||||||
|
|
||||||
//下次轮询事件
|
//下次轮询事件
|
||||||
func (w *WLVS) Next() interface{} {
|
func (w *WLVS) Next() interface{} {
|
||||||
if w.n == 0 {
|
if w.n == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.n == 1 {
|
if w.n == 1 {
|
||||||
return w.nodes[0].Node
|
return w.nodes[0].Node
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
w.i = (w.i + 1) % w.n
|
w.i = (w.i + 1) % w.n
|
||||||
if w.i == 0 {
|
if w.i == 0 {
|
||||||
w.cw = w.cw - w.gcd
|
w.cw = w.cw - w.gcd
|
||||||
if w.cw <= 0 {
|
if w.cw <= 0 {
|
||||||
w.cw = w.maxW
|
w.cw = w.maxW
|
||||||
if w.cw == 0 {
|
if w.cw == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if w.nodes[w.i].Weight >= w.cw {
|
if w.nodes[w.i].Weight >= w.cw {
|
||||||
return w.nodes[w.i].Node
|
return w.nodes[w.i].Node
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//增加权重节点
|
//增加权重节点
|
||||||
func (w *WLVS) Add(node interface{}, weight int) {
|
func (w *WLVS) Add(node interface{}, weight int) {
|
||||||
weighted := &WeightLvs{Node: node, Weight: weight}
|
weighted := &WeightLvs{Node: node, Weight: weight}
|
||||||
if weight > 0 {
|
if weight > 0 {
|
||||||
if w.gcd == 0 {
|
if w.gcd == 0 {
|
||||||
w.gcd = weight
|
w.gcd = weight
|
||||||
w.maxW = weight
|
w.maxW = weight
|
||||||
w.i = -1
|
w.i = -1
|
||||||
w.cw = 0
|
w.cw = 0
|
||||||
} else {
|
} else {
|
||||||
w.gcd = gcd(w.gcd, weight)
|
w.gcd = gcd(w.gcd, weight)
|
||||||
if w.maxW < weight {
|
if w.maxW < weight {
|
||||||
w.maxW = weight
|
w.maxW = weight
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.nodes = append(w.nodes, weighted)
|
w.nodes = append(w.nodes, weighted)
|
||||||
w.n++
|
w.n++
|
||||||
}
|
}
|
||||||
|
|
||||||
func gcd(x, y int) int {
|
func gcd(x, y int) int {
|
||||||
var t int
|
var t int
|
||||||
for {
|
for {
|
||||||
t = (x % y)
|
t = (x % y)
|
||||||
if t > 0 {
|
if t > 0 {
|
||||||
x = y
|
x = y
|
||||||
y = t
|
y = t
|
||||||
} else {
|
} else {
|
||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (w *WLVS) RemoveAll() {
|
func (w *WLVS) RemoveAll() {
|
||||||
w.nodes = w.nodes[:0]
|
w.nodes = w.nodes[:0]
|
||||||
w.n = 0
|
w.n = 0
|
||||||
w.gcd = 0
|
w.gcd = 0
|
||||||
w.maxW = 0
|
w.maxW = 0
|
||||||
w.i = -1
|
w.i = -1
|
||||||
w.cw = 0
|
w.cw = 0
|
||||||
}
|
}
|
||||||
func (w *WLVS) Reset() {
|
func (w *WLVS) Reset() {
|
||||||
w.i = -1
|
w.i = -1
|
||||||
w.cw = 0
|
w.cw = 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ func (this *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
addr := loadBalancer.Next().(string)
|
addr := loadBalancer.Next().(string)
|
||||||
remote, err := url.Parse("http://" + addr)
|
remote, err := url.Parse("http://" + addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err) }
|
panic(err)
|
||||||
|
}
|
||||||
proxy := httputil.NewSingleHostReverseProxy(remote)
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
||||||
proxy.ServeHTTP(w, r)
|
proxy.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user