115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
|
// Package balance...
|
||
|
//
|
||
|
// Description : balance...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2021-11-22 6:58 下午
|
||
|
package balance
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"sync"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
|
||
|
"git.zhangdeman.cn/zhangdeman/gopkg/balance/define"
|
||
|
)
|
||
|
|
||
|
// NewWeightServerRoundRobin ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 6:58 下午 2021/11/22
|
||
|
func NewWeightServerRoundRobin(severList []*define.ServerNode) (IBalance, error) {
|
||
|
if nil == severList || len(severList) == 0 {
|
||
|
return nil, errors.New("sever list is empty")
|
||
|
}
|
||
|
return &WeightServerRoundRobin{
|
||
|
base: base{
|
||
|
lock: &sync.RWMutex{},
|
||
|
severList: severList,
|
||
|
},
|
||
|
effectiveWeight: 0,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// WeightServerRoundRobin 加权轮询
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 6:58 下午 2021/11/22
|
||
|
type WeightServerRoundRobin struct {
|
||
|
base
|
||
|
effectiveWeight float64
|
||
|
}
|
||
|
|
||
|
// GetServerNode ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 7:00 下午 2021/11/22
|
||
|
func (w *WeightServerRoundRobin) GetServerNode(req *http.Request) (*define.ServerNode, error) {
|
||
|
w.RLock()
|
||
|
defer w.RUnlock()
|
||
|
if len(w.severList) == 0 {
|
||
|
return nil, errors.New("sever list is empty")
|
||
|
}
|
||
|
var expectBackendServer *define.ServerNode
|
||
|
for _, backendServer := range w.severList {
|
||
|
// 给每个后端服务增加自身权重
|
||
|
backendServer.CurrentWeight += backendServer.Weight
|
||
|
if expectBackendServer == nil {
|
||
|
expectBackendServer = backendServer
|
||
|
}
|
||
|
if backendServer.CurrentWeight > expectBackendServer.CurrentWeight {
|
||
|
expectBackendServer = backendServer
|
||
|
}
|
||
|
}
|
||
|
// 把选择的后端服务权重减掉总权重
|
||
|
expectBackendServer.CurrentWeight -= w.effectiveWeight
|
||
|
return expectBackendServer, nil
|
||
|
}
|
||
|
|
||
|
// AddServerNode 新添加一个服务器节点
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 10:07 下午 2021/10/20
|
||
|
func (w *WeightServerRoundRobin) AddServerNode(node *define.ServerNode) error {
|
||
|
if len(node.HostIP) == 0 || node.Port <= 0 {
|
||
|
return errors.New("host ip or port is invalid")
|
||
|
}
|
||
|
w.Lock()
|
||
|
defer w.Unlock()
|
||
|
for _, item := range w.severList {
|
||
|
if item.HostIP == node.HostIP && item.Port == node.Port {
|
||
|
return errors.New("host config is already exist")
|
||
|
}
|
||
|
}
|
||
|
w.effectiveWeight += node.Weight
|
||
|
w.severList = append(w.severList, node)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Remove 移除一个节点, force = true , 强制删除, force = false 逻辑删除, 设置状态
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 10:19 下午 2021/10/20
|
||
|
func (w *WeightServerRoundRobin) Remove(hostIP string, port int, force bool) {
|
||
|
w.Lock()
|
||
|
defer w.Unlock()
|
||
|
tmpServerNode := make([]*define.ServerNode, 0)
|
||
|
for _, item := range w.severList {
|
||
|
if item.HostIP == hostIP && item.Port == port {
|
||
|
if force {
|
||
|
continue
|
||
|
}
|
||
|
item.Status = define.ServerNodeStatusRemove
|
||
|
}
|
||
|
tmpServerNode = append(tmpServerNode, item)
|
||
|
}
|
||
|
w.severList = tmpServerNode
|
||
|
}
|