From bd32998c76b20f15e634d8ce39e2363bc62f63c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 20 Oct 2021 22:34:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A7=BB=E9=99=A4=E8=8A=82?= =?UTF-8?q?=E7=82=B9/=E5=A2=9E=E5=8A=A0=E8=8A=82=E7=82=B9/=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E8=8A=82=E7=82=B9=E5=88=97=E8=A1=A8=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- balance/poll.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/balance/poll.go b/balance/poll.go index 1dbf742..c89eac8 100644 --- a/balance/poll.go +++ b/balance/poll.go @@ -74,3 +74,57 @@ func (p *Poll) GetServerNode() (*define.ServerNode, error) { return serverNode, nil } + +// AddServerNode 新添加一个服务器节点 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:07 下午 2021/10/20 +func (p *Poll) AddServerNode(hostIP string, port int) error { + if len(hostIP) == 0 || port <= 0 { + return errors.New("host ip or port is invalid") + } + p.Lock() + defer p.Unlock() + for _, item := range p.serverList { + if item.HostIP == hostIP && item.Port == port { + return errors.New("host config is already exist") + } + } + p.serverList = append(p.serverList, &define.ServerNode{ + HostIP: hostIP, + Port: port, + Status: define.ServerNodeStatusNormal, + }) + return nil +} + +// Remove 移除一个节点, force = true , 强制删除, force = false 逻辑删除, 设置状态 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:19 下午 2021/10/20 +func (p *Poll) Remove(hostIP string, port int, force bool) { + p.Lock() + defer p.Unlock() + tmpServerNode := make([]*define.ServerNode, 0) + for _, item := range p.serverList { + if item.HostIP == hostIP && item.Port == port { + if force { + continue + } + item.Status = define.ServerNodeStatusRemove + } + tmpServerNode = append(tmpServerNode, item) + } + p.serverList = tmpServerNode +} + +// GetServerNodeList 获取服务器节点 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:33 下午 2021/10/20 +func (p *Poll) GetServerNodeList() []*define.ServerNode { + return p.serverList +}