2024-07-18 17:15:15 +00:00
|
|
|
package p2p
|
|
|
|
|
2024-08-13 14:17:18 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand/v2"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
2024-08-07 21:35:44 +00:00
|
|
|
|
2024-07-18 17:15:15 +00:00
|
|
|
const FederatedID = "federated"
|
|
|
|
|
2024-08-07 21:35:44 +00:00
|
|
|
func NetworkID(networkID, serviceID string) string {
|
|
|
|
if networkID != "" {
|
|
|
|
return fmt.Sprintf("%s_%s", networkID, serviceID)
|
|
|
|
}
|
|
|
|
return serviceID
|
|
|
|
}
|
|
|
|
|
2024-07-18 17:15:15 +00:00
|
|
|
type FederatedServer struct {
|
2024-08-13 14:17:18 +00:00
|
|
|
sync.Mutex
|
2024-07-18 17:15:15 +00:00
|
|
|
listenAddr, service, p2ptoken string
|
2024-07-18 21:18:53 +00:00
|
|
|
requestTable map[string]int
|
|
|
|
loadBalanced bool
|
2024-08-13 14:17:18 +00:00
|
|
|
workerTarget string
|
2024-07-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:17:18 +00:00
|
|
|
func NewFederatedServer(listenAddr, service, p2pToken string, loadBalanced bool, workerTarget string) *FederatedServer {
|
2024-07-18 17:15:15 +00:00
|
|
|
return &FederatedServer{
|
2024-07-18 21:18:53 +00:00
|
|
|
listenAddr: listenAddr,
|
|
|
|
service: service,
|
|
|
|
p2ptoken: p2pToken,
|
|
|
|
requestTable: map[string]int{},
|
|
|
|
loadBalanced: loadBalanced,
|
2024-08-13 14:17:18 +00:00
|
|
|
workerTarget: workerTarget,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FederatedServer) RandomServer() string {
|
|
|
|
var tunnelAddresses []string
|
|
|
|
for _, v := range GetAvailableNodes(fs.service) {
|
|
|
|
if v.IsOnline() {
|
|
|
|
tunnelAddresses = append(tunnelAddresses, v.TunnelAddress)
|
|
|
|
} else {
|
|
|
|
delete(fs.requestTable, v.TunnelAddress) // make sure it's not tracked
|
|
|
|
log.Info().Msgf("Node %s is offline", v.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tunnelAddresses) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return tunnelAddresses[rand.IntN(len(tunnelAddresses))]
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
}
|
2024-07-18 21:18:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FederatedServer) SelectLeastUsedServer() string {
|
2024-08-13 14:17:18 +00:00
|
|
|
fs.syncTableStatus()
|
|
|
|
|
|
|
|
fs.Lock()
|
|
|
|
defer fs.Unlock()
|
|
|
|
|
2024-08-17 06:28:52 +00:00
|
|
|
log.Debug().Any("request_table", fs.requestTable).Msgf("SelectLeastUsedServer()")
|
2024-08-13 14:17:18 +00:00
|
|
|
|
2024-07-18 21:18:53 +00:00
|
|
|
// cycle over requestTable and find the entry with the lower number
|
|
|
|
// if there are multiple entries with the same number, select one randomly
|
|
|
|
// if there are no entries, return an empty string
|
|
|
|
var min int
|
|
|
|
var minKey string
|
|
|
|
for k, v := range fs.requestTable {
|
|
|
|
if min == 0 || v < min {
|
|
|
|
min = v
|
|
|
|
minKey = k
|
|
|
|
}
|
|
|
|
}
|
2024-08-17 06:28:52 +00:00
|
|
|
log.Debug().Any("requests_served", min).Any("request_table", fs.requestTable).Msgf("Selected tunnel %s", minKey)
|
2024-08-13 14:17:18 +00:00
|
|
|
|
2024-07-18 21:18:53 +00:00
|
|
|
return minKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FederatedServer) RecordRequest(nodeID string) {
|
2024-08-13 14:17:18 +00:00
|
|
|
fs.Lock()
|
|
|
|
defer fs.Unlock()
|
2024-07-18 21:18:53 +00:00
|
|
|
// increment the counter for the nodeID in the requestTable
|
|
|
|
fs.requestTable[nodeID]++
|
2024-08-13 14:17:18 +00:00
|
|
|
|
2024-08-17 06:28:52 +00:00
|
|
|
log.Debug().Any("request_table", fs.requestTable).Any("request", nodeID).Msgf("Recording request")
|
2024-07-18 21:18:53 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:17:18 +00:00
|
|
|
func (fs *FederatedServer) ensureRecordExist(nodeID string) {
|
2024-07-18 21:18:53 +00:00
|
|
|
// if the nodeID is not in the requestTable, add it with a counter of 0
|
|
|
|
_, ok := fs.requestTable[nodeID]
|
|
|
|
if !ok {
|
|
|
|
fs.requestTable[nodeID] = 0
|
2024-07-18 17:15:15 +00:00
|
|
|
}
|
2024-08-13 14:17:18 +00:00
|
|
|
|
2024-08-17 06:28:52 +00:00
|
|
|
log.Debug().Any("request_table", fs.requestTable).Any("request", nodeID).Msgf("Ensure record exists")
|
2024-07-18 17:15:15 +00:00
|
|
|
}
|