Skip to content

Commit

Permalink
fix: make sure to delete tunnels that might not exist anymore
Browse files Browse the repository at this point in the history
If a worker goes off and on might change tunnel address, and we want to
load balance only on the active tunnels.

Signed-off-by: Ettore Di Giacinto <[email protected]>
  • Loading branch information
mudler committed Aug 13, 2024
1 parent 1906a00 commit 6f39140
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions core/p2p/federated.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,29 @@ func (fs *FederatedServer) RandomServer() string {
return tunnelAddresses[rand.IntN(len(tunnelAddresses))]

Check failure

Code scanning / gosec

Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) Error

Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand)
}

func (fs *FederatedServer) SelectLeastUsedServer() string {
func (fs *FederatedServer) syncTableStatus() {
fs.Lock()
defer fs.Unlock()
currentTunnels := make(map[string]struct{})

for _, v := range GetAvailableNodes(fs.service) {
if v.IsOnline() {
fs.ensureRecordExist(v.TunnelAddress)
} else {
delete(fs.requestTable, v.TunnelAddress)
currentTunnels[v.TunnelAddress] = struct{}{}
}
}

// delete tunnels that don't exist anymore
for t := range currentTunnels {
if _, ok := fs.requestTable[t]; !ok {
delete(fs.requestTable, t)
}
}
}

func (fs *FederatedServer) SelectLeastUsedServer() string {
fs.syncTableStatus()

fs.Lock()
defer fs.Unlock()

Expand Down

0 comments on commit 6f39140

Please sign in to comment.