From 5a4d87c085746ccbcba5dd9fd3a189e273a6556d Mon Sep 17 00:00:00 2001 From: mzz2017 <2017@duck.com> Date: Sun, 12 Feb 2023 00:14:00 +0800 Subject: [PATCH] optimize: re-select algorithm --- component/outbound/dialer/alive_dialer_set.go | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/component/outbound/dialer/alive_dialer_set.go b/component/outbound/dialer/alive_dialer_set.go index 4162b5b..885cda2 100644 --- a/component/outbound/dialer/alive_dialer_set.go +++ b/component/outbound/dialer/alive_dialer_set.go @@ -153,14 +153,14 @@ func (a *AliveDialerSet) NotifyLatencyChange(dialer *Dialer, alive bool) { a.minLatency.latency = latency a.minLatency.dialer = dialer } else if a.minLatency.dialer == dialer { + a.minLatency.latency = latency if !alive || latency > a.minLatency.latency { // Latency increases. - a.minLatency.latency = time.Hour - a.minLatency.dialer = nil + if !alive { + a.minLatency.dialer = nil + } a.calcMinLatency() // Now `a.minLatency.dialer` will be nil if there is no alive dialer. - } else { - a.minLatency.latency = latency } } currentAlive := a.minLatency.dialer != nil @@ -207,14 +207,23 @@ func (a *AliveDialerSet) NotifyLatencyChange(dialer *Dialer, alive bool) { } func (a *AliveDialerSet) calcMinLatency() { + var minLatency = time.Hour + var minDialer *Dialer for _, d := range a.inorderedAliveDialerSet { latency, ok := a.dialerToLatency[d] if !ok { continue } - if latency <= a.minLatency.latency-a.tolerance { - a.minLatency.latency = latency - a.minLatency.dialer = d + if latency < minLatency { + minLatency = latency + 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 + } }