wgengine/magicsock: add hysteresis to endpoint selection

Avoid selecting an endpoint as "better" than the current endpoint if the
total latency improvement is less than 1%. This adds some hysteresis to
avoid flapping between endpoints for a minimal improvement in latency.

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: If8312e1768ea65c4b4d4e13d8de284b3825d7a73
This commit is contained in:
Andrew Dunham
2023-04-21 11:35:16 -04:00
parent c5bf868940
commit bcf7b63d7e
2 changed files with 27 additions and 1 deletions

View File

@@ -4921,6 +4921,28 @@ func betterAddr(a, b addrLatency) bool {
return false
}
}
// If we get here, then both addresses are the same IP type (i.e. both
// IPv4 or both IPv6). All decisions below are made solely on latency.
//
// Determine how much the latencies differ; we ensure the larger
// latency is the denominator, so this fraction will always be <= 1.0.
var latencyFraction float64
if a.latency >= b.latency {
latencyFraction = float64(b.latency) / float64(a.latency)
} else {
latencyFraction = float64(a.latency) / float64(b.latency)
}
// Don't change anything if the latency improvement is less than 1%; we
// want a bit of "stickiness" (a.k.a. hysteresis) to avoid flapping if
// there's two roughly-equivalent endpoints.
if latencyFraction >= 0.99 {
return false
}
// The total difference is >1%, so a is better than b if it's
// lower-latency.
return a.latency < b.latency
}