diff --git a/net/netcheck/netcheck.go b/net/netcheck/netcheck.go index a64c358c5..2b5ec9ad0 100644 --- a/net/netcheck/netcheck.go +++ b/net/netcheck/netcheck.go @@ -18,6 +18,7 @@ import ( "net/http" "net/netip" "runtime" + "slices" "sort" "sync" "syscall" @@ -1399,17 +1400,36 @@ func (c *Client) addReportHistoryAndSetPreferredDERP(rs *reportState, r *Report, } } + // Exclude avoided DERP regions, unless they’re the only ones with latencies. + candidates := make([]int, 0, len(r.RegionLatency)) + // Collect regions that have DERPRegion.Avoid set to false. + // If the DERPRegion isn’t defined, assume the zero value. + regions := dm.Regions() + for regionID := range r.RegionLatency { + if region := regions.Get(regionID); !region.Valid() || !region.Avoid() { + candidates = append(candidates, regionID) + } + } + if len(candidates) == 0 { + candidates = slices.Collect(maps.Keys(r.RegionLatency)) + } + // Then, pick which currently-alive DERP server from the // current report has the best latency over the past maxAge. var ( bestAny time.Duration // global minimum oldRegionCurLatency time.Duration // latency of old PreferredDERP ) - for regionID, d := range r.RegionLatency { + for _, regionID := range candidates { // Scale this report's latency by any scores provided by the // server; we did this for the bestRecent map above, but we // don't mutate the actual reports in-place (in case scores // change), so we need to do it here as well. + d, ok := r.RegionLatency[regionID] + if !ok { + continue + } + if score := scores.Get(regionID); score > 0 { d = time.Duration(float64(d) * score) } diff --git a/net/netcheck/netcheck_test.go b/net/netcheck/netcheck_test.go index 0fd3460fa..0ffda2fb8 100644 --- a/net/netcheck/netcheck_test.go +++ b/net/netcheck/netcheck_test.go @@ -199,6 +199,7 @@ func TestAddReportHistoryAndSetPreferredDERP(t *testing.T) { name string steps []step homeParams *tailcfg.DERPHomeParams + derpRegions map[int]*tailcfg.DERPRegion opts *GetReportOpts forcedDERP int // if non-zero, force this DERP to be the preferred one wantDERP int // want PreferredDERP on final step @@ -281,6 +282,18 @@ func TestAddReportHistoryAndSetPreferredDERP(t *testing.T) { wantPrevLen: 2, wantDERP: 2, // 2 got fast enough }, + { + name: "preferred_derp_hysteresis_avoid_d2", + derpRegions: map[int]*tailcfg.DERPRegion{ + 2: {Avoid: true}, // d2 + }, + steps: []step{ + {0 * time.Second, report("d1", 4, "d2", 5)}, + {1 * time.Second, report("d1", 4, "d2", 1)}, + }, + wantPrevLen: 2, + wantDERP: 1, // 2 got fast enough, but is avoided + }, { name: "derp_home_params", homeParams: &tailcfg.DERPHomeParams{ @@ -451,6 +464,33 @@ func TestAddReportHistoryAndSetPreferredDERP(t *testing.T) { wantPrevLen: 3, wantDERP: 0, }, + { + name: "avoid_all", + derpRegions: map[int]*tailcfg.DERPRegion{ + 1: {Avoid: true}, // d1 + 2: {Avoid: true}, // d2 + 3: {Avoid: true}, // d3 + 4: {Avoid: true}, // d4 + }, + steps: []step{ + {0 * time.Second, report("d1", 3, "d2", 2, "d3", 4, "d4", 5)}, + }, + wantPrevLen: 1, + wantDERP: 2, // all regions are avoided and 2 is best + }, + { + name: "avoid_all_but_one", + derpRegions: map[int]*tailcfg.DERPRegion{ + 1: {Avoid: true}, // d1 + 2: {Avoid: true}, // d2 + 4: {Avoid: true}, // d4 + }, + steps: []step{ + {0 * time.Second, report("d1", 3, "d2", 2, "d3", 4, "d4", 5)}, + }, + wantPrevLen: 1, + wantDERP: 3, // 3 is the only one not avoided + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -459,7 +499,10 @@ func TestAddReportHistoryAndSetPreferredDERP(t *testing.T) { TimeNow: func() time.Time { return fakeTime }, ForcePreferredDERP: tt.forcedDERP, } - dm := &tailcfg.DERPMap{HomeParams: tt.homeParams} + dm := &tailcfg.DERPMap{ + HomeParams: tt.homeParams, + Regions: tt.derpRegions, + } rs := &reportState{ c: c, start: fakeTime,