net/netcheck: keep region latencies from the last full check around

If the latest incremental netcheck result does not have latencies for
some DERP regions, use last full netcheck results instead. Even if those
are out of date, it might be better than choosing a region at random.

Signed-off-by: Anton Tolchanov <[email protected]>
This commit is contained in:
Anton Tolchanov committed 2026-06-08 13:02:53 +01:00
1 parent 254bb6a43c
commit 543ba1ea96
2 files changed
+22 -13

No files matched your search

+18 -13
View File
@@ -8730,29 +8730,34 @@ func randomNode(nodes views.Slice[tailcfg.NodeView], prefer tailcfg.StableNodeID
}
// minLatencyDERPRegion returns the region with the lowest latency value given the last netcheck report.
// If last netcheck report is incremental, it will use the last full report's latency values.
// If there are no latency values, it returns 0.
func minLatencyDERPRegion(regions []int, report *netcheck.Report) int {
const largeDuration time.Duration = math.MaxInt64
get := func(region int) (time.Duration, bool) {
latency, ok := report.RegionLatency[region]
if ok {
return latency, ok
}
lastFull, ok := report.LastFullRegionLatency[region]
if ok {
return lastFull, ok
}
return largeDuration, false
}
min := slices.MinFunc(regions, func(i, j int) int {
const largeDuration time.Duration = math.MaxInt64
iLatency, ok := report.RegionLatency[i]
if !ok {
iLatency = largeDuration
}
jLatency, ok := report.RegionLatency[j]
if !ok {
jLatency = largeDuration
}
iLatency, _ := get(i)
jLatency, _ := get(j)
if c := cmp.Compare(iLatency, jLatency); c != 0 {
return c
}
return cmp.Compare(i, j)
})
latency, ok := report.RegionLatency[min]
if !ok || latency == 0 {
return 0
} else {
if _, ok := get(min); ok {
return min
}
return 0
}
// longLatDistance returns an estimated distance given the geographic coordinates of two locations, in degrees.
+4
View File
@@ -115,6 +115,8 @@ type Report struct {
RegionV4Latency map[int]time.Duration // keyed by DERP Region ID
RegionV6Latency map[int]time.Duration // keyed by DERP Region ID
LastFullRegionLatency map[int]time.Duration // keyed by DERP Region ID, from the last full (non-incremental) report
GlobalV4Counters map[netip.AddrPort]int // number of times the endpoint was observed
GlobalV6Counters map[netip.AddrPort]int // number of times the endpoint was observed
@@ -867,6 +869,8 @@ func (c *Client) GetReport(ctx context.Context, dm *tailcfg.DERPMap, opts *GetRe
c.nextFull = false
c.lastFull = now
metricNumGetReportFull.Add(1)
} else {
rs.report.LastFullRegionLatency = last.RegionLatency
}
rs.incremental = last != nil