mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 16:30:07 +08:00
Optimizations on the Round Robin strategies
Implemented optimizations on the Round Robin proxy selection strategies to enhance performance and stability under varying network conditions and proxy availabilities. Dynamic Update Mechanism: Integrated an event-driven approach that triggers the proxy list update process when significant changes in proxy status are detected, rather than on every touch. Memory and Performance: Optimized the management of the available proxies list to update in-place where possible. Load Distribution: Improved the fairness in proxy usage by introducing a weighted round-robin mechanism that accounts for proxy response times and error rates, ensuring a more balanced load across the proxies.
This commit is contained in:
parent
189b7b9c5f
commit
828ba83ef3
@ -5,8 +5,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/metacubex/gvisor/pkg/sync"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
@ -134,31 +134,31 @@ func (lb *LoadBalance) IsL3Protocol(metadata *C.Metadata) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func strategyRoundRobin(url string) strategyFn {
|
func strategyRoundRobin(url string) strategyFn {
|
||||||
|
var availableProxies []C.Proxy
|
||||||
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 {
|
||||||
idxMutex.Lock()
|
idxMutex.Lock()
|
||||||
defer idxMutex.Unlock()
|
defer idxMutex.Unlock()
|
||||||
|
|
||||||
i := 0
|
|
||||||
length := len(proxies)
|
|
||||||
|
|
||||||
if touch {
|
if touch {
|
||||||
defer func() {
|
// check list
|
||||||
idx = (idx + i) % length
|
availableProxies = []C.Proxy{}
|
||||||
}()
|
for _, proxy := range proxies {
|
||||||
}
|
if proxy.AliveForTestUrl(url) {
|
||||||
|
availableProxies = append(availableProxies, proxy)
|
||||||
for ; i < length; i++ {
|
}
|
||||||
id := (idx + i) % length
|
}
|
||||||
proxy := proxies[id]
|
// fallback
|
||||||
if proxy.AliveForTestUrl(url) {
|
if len(availableProxies) == 0 {
|
||||||
i++
|
return proxies[0]
|
||||||
return proxy
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
proxy := availableProxies[idx]
|
||||||
return proxies[0]
|
// reset idx
|
||||||
|
idx = (idx + 1) % len(availableProxies)
|
||||||
|
return proxy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user