Skip to content

Commit b8b35d9

Browse files
TUN-7002: Randomise first region selection
We previously always preferred region2 as the first region to connect to if both the regions cloudflared connects to have the same number of availabe addresses. This change randomises that choice. The first connection, conn index: 0, can now either connect to region 1 or region 2. More importantly, conn 0 and 2 and 1 and 3 need not belong to the same region.
1 parent 61ccc0b commit b8b35d9

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

edgediscovery/allregions/regions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package allregions
22

33
import (
44
"fmt"
5+
"math/rand"
56

67
"github.com/rs/zerolog"
78
)
@@ -85,6 +86,15 @@ func (rs *Regions) AddrUsedBy(connID int) *EdgeAddr {
8586
// GetUnusedAddr gets an unused addr from the edge, excluding the given addr. Prefer to use addresses
8687
// evenly across both regions.
8788
func (rs *Regions) GetUnusedAddr(excluding *EdgeAddr, connID int) *EdgeAddr {
89+
// If both regions have the same number of available addrs, lets randomise which one
90+
// we pick. The rest of this algorithm will continue to make sure we always use addresses
91+
// evenly across both regions.
92+
if rs.region1.AvailableAddrs() == rs.region2.AvailableAddrs() {
93+
regions := []Region{rs.region1, rs.region2}
94+
firstChoice := rand.Intn(2)
95+
return getAddrs(excluding, connID, &regions[firstChoice], &regions[1-firstChoice])
96+
}
97+
8898
if rs.region1.AvailableAddrs() > rs.region2.AvailableAddrs() {
8999
return getAddrs(excluding, connID, &rs.region1, &rs.region2)
90100
}

0 commit comments

Comments
 (0)