From 543ba1ea96edd824a2c7bf7aca9a4466fe86fc29 Mon Sep 17 00:00:00 2001 From: Anton Tolchanov Date: Mon, 8 Jun 2026 13:02:53 +0100 Subject: [PATCH] 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 --- ipn/ipnlocal/local.go | 31 ++++++++++++++++++------------- net/netcheck/netcheck.go | 4 ++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 20a22e29e..49f70011e 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -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. diff --git a/net/netcheck/netcheck.go b/net/netcheck/netcheck.go index a64c358c5..4c773360e 100644 --- a/net/netcheck/netcheck.go +++ b/net/netcheck/netcheck.go @@ -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