@@ -476,22 +476,38 @@ func (api *API) updateEntitlements(ctx context.Context) error {
476
476
//
477
477
// Two ints are returned, the first is the starting region ID for proxies, and
478
478
// the second is the maximum region ID that already exists in the DERP map.
479
- func getProxyDERPStartingRegionID (derpMap * tailcfg.DERPMap ) (sID int , mID int ) {
480
- maxRegionID := 0
479
+ func getProxyDERPStartingRegionID (derpMap * tailcfg.DERPMap ) (sID int64 , mID int64 ) {
480
+ var maxRegionID int64
481
481
for _ , region := range derpMap .Regions {
482
- if region .RegionID > maxRegionID {
483
- maxRegionID = region .RegionID
482
+ rid := int64 (region .RegionID )
483
+ if rid > maxRegionID {
484
+ maxRegionID = rid
484
485
}
485
486
}
486
487
if maxRegionID < 0 {
487
488
maxRegionID = 0
488
489
}
489
490
490
491
// Round to the nearest 10,000 with a sufficient buffer of at least 2,000.
492
+ // The buffer allows for future "fixed" regions to be added to the base DERP
493
+ // map without conflicting with proxy region IDs (standard DERP maps usually
494
+ // use incrementing IDs for new regions).
495
+ //
496
+ // Example:
497
+ // maxRegionID = -2_000 -> startingRegionID = 10_000
498
+ // maxRegionID = 8_000 -> startingRegionID = 10_000
499
+ // maxRegionID = 8_500 -> startingRegionID = 20_000
500
+ // maxRegionID = 12_000 -> startingRegionID = 20_000
501
+ // maxRegionID = 20_000 -> startingRegionID = 30_000
491
502
const roundStartingRegionID = 10_000
492
503
const startingRegionIDBuffer = 2_000
504
+ // Add the buffer first.
493
505
startingRegionID := maxRegionID + startingRegionIDBuffer
494
- startingRegionID = int (math .Ceil (float64 (startingRegionID )/ roundStartingRegionID ) * roundStartingRegionID )
506
+ // Round UP to the nearest 10,000. Go's math.Ceil rounds up to the nearest
507
+ // integer, so we need to divide by 10,000 first and then multiply by
508
+ // 10,000.
509
+ startingRegionID = int64 (math .Ceil (float64 (startingRegionID )/ roundStartingRegionID ) * roundStartingRegionID )
510
+ // This should never be hit but it's here just in case.
495
511
if startingRegionID < roundStartingRegionID {
496
512
startingRegionID = roundStartingRegionID
497
513
}
@@ -565,7 +581,7 @@ func derpMapper(logger slog.Logger, proxyHealth *proxyhealth.ProxyHealth) func(*
565
581
// This should be impossible to hit as the IDs are enforced to be
566
582
// unique by the database and the computed ID is greater than any
567
583
// existing ID in the DERP map.
568
- regionID := startingRegionID + int (status .Proxy .RegionID )
584
+ regionID := int ( startingRegionID ) + int (status .Proxy .RegionID )
569
585
regionCode := fmt .Sprintf ("coder_%s" , strings .ToLower (status .Proxy .Name ))
570
586
for _ , r := range derpMap .Regions {
571
587
if r .RegionID == regionID || r .RegionCode == regionCode {
0 commit comments