Compare commits

...

4 Commits

Author SHA1 Message Date
Aubrey Yang
59f0e9fbcb
Merge 256e9d8c3d into 063836fe5d 2024-06-06 05:07:05 +08:00
wwqgtxx
063836fe5d chore: sync hysteria2 bbr changes
e0e75c4630
2024-06-05 11:56:27 +08:00
Aubrey Yang
256e9d8c3d
Update loadbalance.go 2024-04-17 18:21:59 +09:00
Aubrey Yang
828ba83ef3
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.
2024-04-17 18:20:30 +09:00
2 changed files with 23 additions and 24 deletions

View File

@ -5,8 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"sync"
"net"
"time"
"github.com/metacubex/mihomo/adapter/outbound"
@ -134,33 +134,33 @@ func (lb *LoadBalance) IsL3Protocol(metadata *C.Metadata) bool {
}
func strategyRoundRobin(url string) strategyFn {
var availableProxies []C.Proxy
idx := 0
idxMutex := sync.Mutex{}
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
idxMutex.Lock()
defer idxMutex.Unlock()
i := 0
length := len(proxies)
if touch {
defer func() {
idx = (idx + i) % length
}()
}
for ; i < length; i++ {
id := (idx + i) % length
proxy := proxies[id]
// check list
availableProxies = []C.Proxy{}
for _, proxy := range proxies {
if proxy.AliveForTestUrl(url) {
i++
return proxy
availableProxies = append(availableProxies, proxy)
}
}
// fallback
if len(availableProxies) == 0 {
return proxies[0]
}
}
proxy := availableProxies[idx]
// reset idx
idx = (idx + 1) % len(availableProxies)
return proxy
}
}
func strategyConsistentHashing(url string) strategyFn {
maxRetry := 5

View File

@ -62,7 +62,7 @@ const (
// Flag.
defaultStartupFullLossCount = 8
quicBbr2DefaultLossThreshold = 0.02
maxBbrBurstPackets = 3
maxBbrBurstPackets = 10
)
type bbrMode int
@ -334,6 +334,8 @@ func (b *bbrSender) OnPacketSent(
}
b.sampler.OnPacketSent(sentTime, packetNumber, bytes, bytesInFlight, isRetransmittable)
b.maybeAppLimited(bytesInFlight)
}
// CanSend implements the SendAlgorithm interface.
@ -413,8 +415,6 @@ func (b *bbrSender) OnCongestionEventEx(priorInFlight congestion.ByteCount, even
// packet in lost_packets.
var lastPacketSendState sendTimeState
b.maybeApplimited(priorInFlight)
// Update bytesInFlight
b.bytesInFlight = priorInFlight
for _, p := range ackedPackets {
@ -541,7 +541,7 @@ func (b *bbrSender) setDrainGain(drainGain float64) {
b.drainGain = drainGain
}
// What's the current estimated bandwidth in bytes per second.
// Get the current bandwidth estimate. Note that Bandwidth is in bits per second.
func (b *bbrSender) bandwidthEstimate() Bandwidth {
return b.maxBandwidth.GetBest()
}
@ -700,14 +700,13 @@ func (b *bbrSender) checkIfFullBandwidthReached(lastPacketSendState *sendTimeSta
}
}
func (b *bbrSender) maybeApplimited(bytesInFlight congestion.ByteCount) {
func (b *bbrSender) maybeAppLimited(bytesInFlight congestion.ByteCount) {
congestionWindow := b.GetCongestionWindow()
if bytesInFlight >= congestionWindow {
return
}
availableBytes := congestionWindow - bytesInFlight
drainLimited := b.mode == bbrModeDrain && bytesInFlight > congestionWindow/2
if !drainLimited || availableBytes > maxBbrBurstPackets*b.maxDatagramSize {
if availableBytes > maxBbrBurstPackets*b.maxDatagramSize {
b.sampler.OnAppLimited()
}
}