77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
|
// Package balance ...
|
||
|
//
|
||
|
// Description : 轮询机制的负载均衡
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2021-10-19 12:35 下午
|
||
|
package balance
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"git.zhangdeman.cn/zhangdeman/gopkg/balance/define"
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
// NewPoll 获取轮询实例
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 12:40 下午 2021/10/19
|
||
|
func NewPoll(serverList []*define.ServerNode) (*Poll, error) {
|
||
|
if nil == serverList || len(serverList) == 0 {
|
||
|
return nil, errors.New("server list is empty")
|
||
|
}
|
||
|
return &Poll{
|
||
|
base: base{
|
||
|
lock: &sync.RWMutex{},
|
||
|
},
|
||
|
serverList: serverList,
|
||
|
currentServerIndex: 0,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// Poll ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 12:41 下午 2021/10/19
|
||
|
type Poll struct {
|
||
|
base
|
||
|
serverList []*define.ServerNode
|
||
|
currentServerIndex int
|
||
|
}
|
||
|
|
||
|
// GetServerNode 获取服务节点
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 12:43 下午 2021/10/19
|
||
|
func (p *Poll) GetServerNode() (*define.ServerNode, error) {
|
||
|
if len(p.serverList) == 0 {
|
||
|
return nil, errors.New("server list is empty")
|
||
|
}
|
||
|
p.Lock()
|
||
|
defer p.Unlock()
|
||
|
var (
|
||
|
serverNode *define.ServerNode
|
||
|
)
|
||
|
// 循环次数
|
||
|
loopTimes := 0
|
||
|
for loopTimes < len(p.serverList) {
|
||
|
loopTimes++
|
||
|
p.currentServerIndex = (p.currentServerIndex + 1) % len(p.serverList)
|
||
|
if serverNode = p.serverList[p.currentServerIndex]; serverNode.Status != define.ServerNodeStatusNormal {
|
||
|
continue
|
||
|
}
|
||
|
break
|
||
|
}
|
||
|
|
||
|
if nil == serverNode || serverNode.Status != define.ServerNodeStatusNormal {
|
||
|
return nil, errors.New("has no server node be used")
|
||
|
}
|
||
|
|
||
|
return serverNode, nil
|
||
|
}
|