Cached rooms by ID on load balancer

This commit is contained in:
Derek S 2021-04-09 02:34:06 -05:00
parent a9b48465fe
commit 8b6919abd3
3 changed files with 8 additions and 22 deletions

View file

@ -77,11 +77,17 @@ namespace LightReflectiveMirror.LoadBalancing
ClearAllServersLists(); ClearAllServersLists();
List<Room> requestedRooms; List<Room> requestedRooms;
Program.cachedRooms.Clear();
for (int i = 0; i < relays.Count; i++) for (int i = 0; i < relays.Count; i++)
{ {
requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort); requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort);
_regionRooms[LRMRegions.Any].AddRange(requestedRooms); _regionRooms[LRMRegions.Any].AddRange(requestedRooms);
_regionRooms[relays[i].Key.serverRegion].AddRange(requestedRooms); _regionRooms[relays[i].Key.serverRegion].AddRange(requestedRooms);
for (int x = 0; x < requestedRooms.Count; x++)
if (!Program.cachedRooms.TryAdd(requestedRooms[x].serverId, requestedRooms[x]))
Logger.ForceLogMessage("Conflicting Rooms! (That's ok)", ConsoleColor.Yellow);
} }
CacheAllServers(); CacheAllServers();

View file

@ -15,6 +15,7 @@ namespace LightReflectiveMirror.LoadBalancing
/// Keeps track of all the LRM nodes registered to the Load Balancer. /// Keeps track of all the LRM nodes registered to the Load Balancer.
/// </summary> /// </summary>
public Dictionary<RelayAddress, RelayServerInfo> availableRelayServers = new(); public Dictionary<RelayAddress, RelayServerInfo> availableRelayServers = new();
public static Dictionary<string, Room> cachedRooms = new();
private int _pingDelay = 10000; private int _pingDelay = 10000;
public static bool showDebugLogs = false; public static bool showDebugLogs = false;

View file

@ -38,30 +38,9 @@ namespace LightReflectiveMirror.LoadBalancing
randomID = new string(Enumerable.Repeat(chars, LENGTH) randomID = new string(Enumerable.Repeat(chars, LENGTH)
.Select(s => s[random.Next(s.Length)]).ToArray()); .Select(s => s[random.Next(s.Length)]).ToArray());
} }
while (DoesServerIdExist(randomID)); while (cachedRooms.ContainsKey(randomID));
return randomID; return randomID;
} }
/// <summary>
/// Checks if a server id already is in use.
/// </summary>
/// <param name="id">The ID to check for</param>
/// <returns></returns>
bool DoesServerIdExist(string id)
{
var infos = availableRelayServers.Values;
foreach (var info in infos)
{
foreach (var server in info.serversConnectedToRelay)
{
if (server.serverId == id)
return true;
}
}
return false;
}
} }
} }