Added LoadBalancer auto reconnect for LRM nodes

This commit is contained in:
Derek S 2021-04-06 17:00:51 -05:00
parent 35731871f8
commit b92ce3010b
2 changed files with 14 additions and 1 deletions

View file

@ -25,6 +25,8 @@ namespace LightReflectiveMirror.Endpoints
{ {
private static string _cachedServerList = "[]"; private static string _cachedServerList = "[]";
private static string _cachedCompressedServerList; private static string _cachedCompressedServerList;
public static DateTime lastPing = DateTime.Now;
private static List<Room> _rooms { get => Program.instance.GetRooms().Where(x => x.isPublic).ToList(); } private static List<Room> _rooms { get => Program.instance.GetRooms().Where(x => x.isPublic).ToList(); }
private RelayStats _stats { get => new RelayStats private RelayStats _stats { get => new RelayStats
@ -44,6 +46,7 @@ namespace LightReflectiveMirror.Endpoints
[RestRoute("Get", "/api/stats")] [RestRoute("Get", "/api/stats")]
public async Task Stats(IHttpContext context) public async Task Stats(IHttpContext context)
{ {
lastPing = DateTime.Now;
string json = JsonConvert.SerializeObject(_stats, Formatting.Indented); string json = JsonConvert.SerializeObject(_stats, Formatting.Indented);
await context.Response.SendResponseAsync(json); await context.Response.SendResponseAsync(json);
} }

View file

@ -167,7 +167,7 @@ namespace LightReflectiveMirror
Environment.Exit(0); Environment.Exit(0);
} }
if(conf.UseLoadBalancer) if (conf.UseLoadBalancer)
await RegisterSelfToLoadBalancer(); await RegisterSelfToLoadBalancer();
} }
@ -185,6 +185,15 @@ namespace LightReflectiveMirror
for(int i = 0; i < _currentConnections.Count; i++) for(int i = 0; i < _currentConnections.Count; i++)
transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(new byte[] { 200 })); transport.ServerSend(_currentConnections[i], 0, new ArraySegment<byte>(new byte[] { 200 }));
if (conf.UseLoadBalancer)
{
if (Endpoint.lastPing.AddSeconds(60) > DateTime.Now)
{
// Dont await that on main thread. It would cause a lag spike for clients.
RegisterSelfToLoadBalancer();
}
}
GC.Collect(); GC.Collect();
} }
@ -194,6 +203,7 @@ namespace LightReflectiveMirror
private async Task<bool> RegisterSelfToLoadBalancer() private async Task<bool> RegisterSelfToLoadBalancer()
{ {
Endpoint.lastPing = DateTime.Now;
try try
{ {
// replace hard coded value for config value later // replace hard coded value for config value later