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 <mudler@localai.io>
  • Loading branch information
mudler committed Aug 13, 2024
1 parent 1906a00 commit 0d6637e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 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))]
}

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 fs.requestTable {
if _, ok := currentTunnels[t]; !ok {
delete(fs.requestTable, t)
}
}
}

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

fs.Lock()
defer fs.Unlock()

Expand Down Expand Up @@ -94,8 +108,6 @@ func (fs *FederatedServer) RecordRequest(nodeID string) {
}

func (fs *FederatedServer) ensureRecordExist(nodeID string) {
fs.Lock()
defer fs.Unlock()
// if the nodeID is not in the requestTable, add it with a counter of 0
_, ok := fs.requestTable[nodeID]
if !ok {
Expand Down

0 comments on commit 0d6637e

Please sign in to comment.