From 60160c44916dea9a0690bea3b12cd2a0cf366ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Thu, 1 Apr 2021 20:25:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AE=E8=AF=A2=E7=AE=97=E6=B3=95=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=B4=A2=E5=BC=95=E8=B6=8A=E7=95=8C=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dispatch/round_robin.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dispatch/round_robin.go b/dispatch/round_robin.go index 2cda071..9604c68 100644 --- a/dispatch/round_robin.go +++ b/dispatch/round_robin.go @@ -49,6 +49,10 @@ func (r *RoundRobin) Get(nodeList []*define.SeverNode) (string, *define.Error) { defer func() { _ = r.lock.Unlock() }() + if r.nextNodeIndex >= len(nodeList) { + // 记录过索引之后, 在下次访问之前, 可能移除了某些节点, 所以要检测越界 + r.nextNodeIndex = len(nodeList) - 1 + } node := fmt.Sprintf("%s:%d", nodeList[r.nextNodeIndex].Host, nodeList[r.nextNodeIndex].Port) r.nextNodeIndex = (r.nextNodeIndex + 1) % len(nodeList) return node, nil