From b92ce3010b64ef30349df9254b5ab532ce34f1e0 Mon Sep 17 00:00:00 2001 From: Derek S <44935661+Derek-R-S@users.noreply.github.com> Date: Tue, 6 Apr 2021 17:00:51 -0500 Subject: [PATCH] Added LoadBalancer auto reconnect for LRM nodes --- ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Endpoint.cs | 3 +++ .../LRM/Program/Program.cs | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Endpoint.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Endpoint.cs index 8a44057..e96156e 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Endpoint.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Endpoint.cs @@ -25,6 +25,8 @@ namespace LightReflectiveMirror.Endpoints { private static string _cachedServerList = "[]"; private static string _cachedCompressedServerList; + public static DateTime lastPing = DateTime.Now; + private static List _rooms { get => Program.instance.GetRooms().Where(x => x.isPublic).ToList(); } private RelayStats _stats { get => new RelayStats @@ -44,6 +46,7 @@ namespace LightReflectiveMirror.Endpoints [RestRoute("Get", "/api/stats")] public async Task Stats(IHttpContext context) { + lastPing = DateTime.Now; string json = JsonConvert.SerializeObject(_stats, Formatting.Indented); await context.Response.SendResponseAsync(json); } diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs index 000c370..bf18d58 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/Program/Program.cs @@ -167,7 +167,7 @@ namespace LightReflectiveMirror Environment.Exit(0); } - if(conf.UseLoadBalancer) + if (conf.UseLoadBalancer) await RegisterSelfToLoadBalancer(); } @@ -185,6 +185,15 @@ namespace LightReflectiveMirror for(int i = 0; i < _currentConnections.Count; i++) transport.ServerSend(_currentConnections[i], 0, new ArraySegment(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(); } @@ -194,6 +203,7 @@ namespace LightReflectiveMirror private async Task RegisterSelfToLoadBalancer() { + Endpoint.lastPing = DateTime.Now; try { // replace hard coded value for config value later