From b2d1cea7595975f69d4e440d89e9beca531cf1bf Mon Sep 17 00:00:00 2001 From: Ovear Date: Fri, 17 Feb 2023 16:31:00 +0800 Subject: [PATCH] fix: RoundRobin strategy of load balance when called multiple times (#390) --- adapter/outboundgroup/loadbalance.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/adapter/outboundgroup/loadbalance.go b/adapter/outboundgroup/loadbalance.go index 7dc4d3d3c..48bd49942 100644 --- a/adapter/outboundgroup/loadbalance.go +++ b/adapter/outboundgroup/loadbalance.go @@ -115,11 +115,20 @@ func (lb *LoadBalance) SupportUDP() bool { } func strategyRoundRobin() strategyFn { + flag := true idx := 0 return func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy { length := len(proxies) for i := 0; i < length; i++ { - idx = (idx + 1) % length + flag = !flag + if flag { + idx = (idx - 1) % length + } else { + idx = (idx + 2) % length + } + if idx < 0 { + idx = idx + length + } proxy := proxies[idx] if proxy.Alive() { return proxy