optimize: re-select algorithm

This commit is contained in:
mzz2017 2023-02-12 00:14:00 +08:00
parent e269c4bf60
commit 5a4d87c085

View File

@ -153,14 +153,14 @@ func (a *AliveDialerSet) NotifyLatencyChange(dialer *Dialer, alive bool) {
a.minLatency.latency = latency a.minLatency.latency = latency
a.minLatency.dialer = dialer a.minLatency.dialer = dialer
} else if a.minLatency.dialer == dialer { } else if a.minLatency.dialer == dialer {
a.minLatency.latency = latency
if !alive || latency > a.minLatency.latency { if !alive || latency > a.minLatency.latency {
// Latency increases. // Latency increases.
a.minLatency.latency = time.Hour if !alive {
a.minLatency.dialer = nil a.minLatency.dialer = nil
}
a.calcMinLatency() a.calcMinLatency()
// Now `a.minLatency.dialer` will be nil if there is no alive dialer. // Now `a.minLatency.dialer` will be nil if there is no alive dialer.
} else {
a.minLatency.latency = latency
} }
} }
currentAlive := a.minLatency.dialer != nil currentAlive := a.minLatency.dialer != nil
@ -207,14 +207,23 @@ func (a *AliveDialerSet) NotifyLatencyChange(dialer *Dialer, alive bool) {
} }
func (a *AliveDialerSet) calcMinLatency() { func (a *AliveDialerSet) calcMinLatency() {
var minLatency = time.Hour
var minDialer *Dialer
for _, d := range a.inorderedAliveDialerSet { for _, d := range a.inorderedAliveDialerSet {
latency, ok := a.dialerToLatency[d] latency, ok := a.dialerToLatency[d]
if !ok { if !ok {
continue continue
} }
if latency <= a.minLatency.latency-a.tolerance { if latency < minLatency {
a.minLatency.latency = latency minLatency = latency
a.minLatency.dialer = d minDialer = d
} }
} }
if a.minLatency.dialer == nil {
a.minLatency.latency = minLatency
a.minLatency.dialer = minDialer
} else if minDialer != nil && minLatency <= a.minLatency.latency-a.tolerance {
a.minLatency.latency = minLatency
a.minLatency.dialer = minDialer
}
} }