MCPcopy Index your code
hub / github.com/coder/coder / getProxyDERPStartingRegionID

Function getProxyDERPStartingRegionID

enterprise/coderd/coderd.go:1130–1167  ·  view source on GitHub ↗

getProxyDERPStartingRegionID returns the starting region ID that should be used for workspace proxies. A proxy's actual region ID is the return value from this function + it's RegionID field. Two ints are returned, the first is the starting region ID for proxies, and the second is the maximum regio

(derpMap *tailcfg.DERPMap)

Source from the content-addressed store, hash-verified

1128// Two ints are returned, the first is the starting region ID for proxies, and
1129// the second is the maximum region ID that already exists in the DERP map.
1130func getProxyDERPStartingRegionID(derpMap *tailcfg.DERPMap) (sID int64, mID int64) {
1131 var maxRegionID int64
1132 for _, region := range derpMap.Regions {
1133 rid := int64(region.RegionID)
1134 if rid > maxRegionID {
1135 maxRegionID = rid
1136 }
1137 }
1138 if maxRegionID < 0 {
1139 maxRegionID = 0
1140 }
1141
1142 // Round to the nearest 10,000 with a sufficient buffer of at least 2,000.
1143 // The buffer allows for future "fixed" regions to be added to the base DERP
1144 // map without conflicting with proxy region IDs (standard DERP maps usually
1145 // use incrementing IDs for new regions).
1146 //
1147 // Example:
1148 // maxRegionID = -2_000 -> startingRegionID = 10_000
1149 // maxRegionID = 8_000 -> startingRegionID = 10_000
1150 // maxRegionID = 8_500 -> startingRegionID = 20_000
1151 // maxRegionID = 12_000 -> startingRegionID = 20_000
1152 // maxRegionID = 20_000 -> startingRegionID = 30_000
1153 const roundStartingRegionID = 10_000
1154 const startingRegionIDBuffer = 2_000
1155 // Add the buffer first.
1156 startingRegionID := maxRegionID + startingRegionIDBuffer
1157 // Round UP to the nearest 10,000. Go's math.Ceil rounds up to the nearest
1158 // integer, so we need to divide by 10,000 first and then multiply by
1159 // 10,000.
1160 startingRegionID = int64(math.Ceil(float64(startingRegionID)/roundStartingRegionID) * roundStartingRegionID)
1161 // This should never be hit but it's here just in case.
1162 if startingRegionID < roundStartingRegionID {
1163 startingRegionID = roundStartingRegionID
1164 }
1165
1166 return startingRegionID, maxRegionID
1167}
1168
1169var (
1170 lastDerpConflictMutex sync.Mutex

Callers 2

derpMapperFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected