Skip to content

Commit 0d6637e

Browse files
committed
fix: make sure to delete tunnels that might not exist anymore
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]>
1 parent 1906a00 commit 0d6637e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

core/p2p/federated.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,29 @@ func (fs *FederatedServer) RandomServer() string {
5454
return tunnelAddresses[rand.IntN(len(tunnelAddresses))]
5555
}
5656

57-
func (fs *FederatedServer) SelectLeastUsedServer() string {
57+
func (fs *FederatedServer) syncTableStatus() {
58+
fs.Lock()
59+
defer fs.Unlock()
60+
currentTunnels := make(map[string]struct{})
61+
5862
for _, v := range GetAvailableNodes(fs.service) {
5963
if v.IsOnline() {
6064
fs.ensureRecordExist(v.TunnelAddress)
61-
} else {
62-
delete(fs.requestTable, v.TunnelAddress)
65+
currentTunnels[v.TunnelAddress] = struct{}{}
6366
}
6467
}
6568

69+
// delete tunnels that don't exist anymore
70+
for t := range fs.requestTable {
71+
if _, ok := currentTunnels[t]; !ok {
72+
delete(fs.requestTable, t)
73+
}
74+
}
75+
}
76+
77+
func (fs *FederatedServer) SelectLeastUsedServer() string {
78+
fs.syncTableStatus()
79+
6680
fs.Lock()
6781
defer fs.Unlock()
6882

@@ -94,8 +108,6 @@ func (fs *FederatedServer) RecordRequest(nodeID string) {
94108
}
95109

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

0 commit comments

Comments
 (0)