增加随机负载均衡策略

This commit is contained in:
2021-11-22 18:34:37 +08:00
parent ad8c3dc47d
commit 39e3cdef91
2 changed files with 64 additions and 9 deletions

View File

@ -49,21 +49,17 @@ type Poll struct {
//
// Date : 12:43 下午 2021/10/19
func (p *Poll) GetServerNode(req *http.Request) (*define.ServerNode, error) {
severList := p.GetServerNodeList()
if len(severList) == 0 {
return nil, errors.New("server list is empty")
}
p.Lock()
defer p.Unlock()
p.RLock()
defer p.RUnlock()
var (
serverNode *define.ServerNode
)
// 循环次数
loopTimes := 0
for loopTimes < len(severList) {
for loopTimes < len(p.severList) {
loopTimes++
p.currentServerIndex = (p.currentServerIndex + 1) % len(severList)
if serverNode = severList[p.currentServerIndex]; serverNode.Status != define.ServerNodeStatusNormal {
p.currentServerIndex = (p.currentServerIndex + 1) % len(p.severList)
if serverNode = p.severList[p.currentServerIndex]; serverNode.Status != define.ServerNodeStatusNormal {
continue
}
break