2021-10-19 14:48:46 +08:00
|
|
|
// Package balance...
|
|
|
|
//
|
|
|
|
// Description : balance...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-10-19 2:26 下午
|
|
|
|
package balance
|
|
|
|
|
2021-11-22 17:21:05 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/gopkg/balance/define"
|
|
|
|
)
|
2021-10-19 14:48:46 +08:00
|
|
|
|
|
|
|
// base ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:26 下午 2021/10/19
|
|
|
|
type base struct {
|
2021-11-22 17:21:05 +08:00
|
|
|
lock *sync.RWMutex
|
|
|
|
severList []*define.ServerNode
|
2021-10-19 14:48:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:27 下午 2021/10/19
|
|
|
|
func (b *base) Lock() {
|
|
|
|
b.lock.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:27 下午 2021/10/19
|
|
|
|
func (b *base) Unlock() {
|
|
|
|
b.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLock ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:28 下午 2021/10/19
|
|
|
|
func (b *base) RLock() {
|
|
|
|
b.lock.RLock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:28 下午 2021/10/19
|
|
|
|
func (b *base) RUnlock() {
|
|
|
|
b.lock.RUnlock()
|
|
|
|
}
|
2021-11-22 17:21:05 +08:00
|
|
|
|
|
|
|
// AddServerNode 新添加一个服务器节点
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 10:07 下午 2021/10/20
|
2021-11-22 19:12:49 +08:00
|
|
|
func (b *base) AddServerNode(node *define.ServerNode) error {
|
|
|
|
if len(node.HostIP) == 0 || node.Port <= 0 {
|
2021-11-22 17:21:05 +08:00
|
|
|
return errors.New("host ip or port is invalid")
|
|
|
|
}
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
for _, item := range b.severList {
|
2021-11-22 19:12:49 +08:00
|
|
|
if item.HostIP == node.HostIP && item.Port == node.Port {
|
2021-11-22 17:21:05 +08:00
|
|
|
return errors.New("host config is already exist")
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 19:12:49 +08:00
|
|
|
b.severList = append(b.severList, node)
|
2021-11-22 17:21:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove 移除一个节点, force = true , 强制删除, force = false 逻辑删除, 设置状态
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 10:19 下午 2021/10/20
|
|
|
|
func (b *base) Remove(hostIP string, port int, force bool) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
tmpServerNode := make([]*define.ServerNode, 0)
|
|
|
|
for _, item := range b.severList {
|
|
|
|
if item.HostIP == hostIP && item.Port == port {
|
|
|
|
if force {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
item.Status = define.ServerNodeStatusRemove
|
|
|
|
}
|
|
|
|
tmpServerNode = append(tmpServerNode, item)
|
|
|
|
}
|
|
|
|
b.severList = tmpServerNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerNodeList 获取服务器节点
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 10:33 下午 2021/10/20
|
|
|
|
func (b *base) GetServerNodeList() []*define.ServerNode {
|
|
|
|
return b.severList
|
|
|
|
}
|