fix: strategyRoundRobin not begin with zero
This commit is contained in:
parent
8521485872
commit
5ea4f0ed0b
1 changed files with 10 additions and 8 deletions
|
@ -131,21 +131,23 @@ func strategyRoundRobin() strategyFn {
|
||||||
idx := 0
|
idx := 0
|
||||||
idxMutex := sync.Mutex{}
|
idxMutex := sync.Mutex{}
|
||||||
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
||||||
id := idx // value could be wrong due to no lock, but don't care if we don't touch
|
idxMutex.Lock()
|
||||||
|
defer idxMutex.Unlock()
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
length := len(proxies)
|
||||||
|
|
||||||
if touch {
|
if touch {
|
||||||
idxMutex.Lock()
|
|
||||||
defer idxMutex.Unlock()
|
|
||||||
id = idx // get again by lock's protect, so it must be right
|
|
||||||
defer func() {
|
defer func() {
|
||||||
idx = id
|
idx = (idx + i) % length
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
length := len(proxies)
|
for ; i < length; i++ {
|
||||||
for i := 0; i < length; i++ {
|
id := (idx + i) % length
|
||||||
id = (id + 1) % length
|
|
||||||
proxy := proxies[id]
|
proxy := proxies[id]
|
||||||
if proxy.Alive() {
|
if proxy.Alive() {
|
||||||
|
i++
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue