This commit is contained in:
Derek S 2021-04-06 16:09:08 -05:00
commit f6ed0bb481
4 changed files with 100 additions and 35 deletions

View file

@ -0,0 +1,50 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace LightReflectiveMirror.LoadBalancing
{
// for stats
[Serializable]
public struct RelayServerInfo
{
public int ConnectedClients;
public int RoomCount;
public int PublicRoomCount;
public TimeSpan Uptime;
}
[Serializable]
internal struct LoadBalancerStats
{
public int NodeCount;
public TimeSpan Uptime;
public long CCU;
public long TotalServerCount;
}
// container for relay address info
[JsonObject(MemberSerialization.OptOut)]
public struct RelayAddress
{
public ushort Port;
public ushort EndpointPort;
public string Address;
[JsonIgnore]
public string EndpointAddress;
}
[Serializable]
public struct Room
{
public int serverId;
public int hostId;
public string serverName;
public string serverData;
public bool isPublic;
public int maxPlayers;
public List<int> clients;
public RelayAddress relayInfo;
}
}

View file

@ -17,6 +17,17 @@ namespace LightReflectiveMirror.LoadBalancing
[RestResource] [RestResource]
public class Endpoint public class Endpoint
{ {
private LoadBalancerStats _stats
{
get => new()
{
NodeCount = Program.instance.availableRelayServers.Count,
Uptime = DateTime.Now - Program.startupTime,
CCU = Program.instance.GetTotalCCU(),
TotalServerCount = Program.instance.GetTotalServers(),
};
}
/// <summary> /// <summary>
/// Sent from an LRM server node /// Sent from an LRM server node
/// adds it to the list if authenticated. /// adds it to the list if authenticated.
@ -136,6 +147,17 @@ namespace LightReflectiveMirror.LoadBalancing
else else
await context.Response.SendResponseAsync(HttpStatusCode.NoContent); await context.Response.SendResponseAsync(HttpStatusCode.NoContent);
} }
/// <summary>
/// Returns stats. you're welcome
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
[RestRoute("Get", "/api/stats/")]
public async Task GetStats(IHttpContext context)
{
await context.Response.SendResponseAsync(JsonConvert.SerializeObject(_stats));
}
} }
#region Startup #region Startup

View file

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace LightReflectiveMirror.LoadBalancing namespace LightReflectiveMirror.LoadBalancing
{ {
class Program partial class Program
{ {
/// <summary> /// <summary>
/// Keeps track of all available relays. /// Keeps track of all available relays.
@ -18,6 +18,7 @@ namespace LightReflectiveMirror.LoadBalancing
private int _pingDelay = 10000; private int _pingDelay = 10000;
public static bool showDebugLogs = false; public static bool showDebugLogs = false;
public static DateTime startupTime;
const string API_PATH = "/api/stats"; const string API_PATH = "/api/stats";
readonly string CONFIG_PATH = System.Environment.GetEnvironmentVariable("LRM_LB_CONFIG_PATH") ?? "config.json"; readonly string CONFIG_PATH = System.Environment.GetEnvironmentVariable("LRM_LB_CONFIG_PATH") ?? "config.json";
@ -30,6 +31,7 @@ namespace LightReflectiveMirror.LoadBalancing
{ {
WriteTitle(); WriteTitle();
instance = this; instance = this;
startupTime = DateTime.Now;
if (!File.Exists(CONFIG_PATH)) if (!File.Exists(CONFIG_PATH))
{ {
@ -195,38 +197,4 @@ namespace LightReflectiveMirror.LoadBalancing
} }
// for stats
[Serializable]
public struct RelayServerInfo
{
public int ConnectedClients;
public int RoomCount;
public int PublicRoomCount;
public TimeSpan Uptime;
}
// container for relay address info
[JsonObject(MemberSerialization.OptOut)]
public struct RelayAddress
{
public ushort Port;
public ushort EndpointPort;
public string Address;
[JsonIgnore]
public string EndpointAddress;
}
[Serializable]
public struct Room
{
public int serverId;
public int hostId;
public string serverName;
public string serverData;
public bool isPublic;
public int maxPlayers;
public List<int> clients;
public RelayAddress relayInfo;
}
} }

View file

@ -0,0 +1,25 @@
namespace LightReflectiveMirror.LoadBalancing
{
partial class Program
{
public long GetTotalCCU()
{
long temp = 0;
foreach (var item in availableRelayServers)
temp += item.Value.ConnectedClients;
return temp;
}
public long GetTotalServers()
{
int temp = 0;
foreach (var item in availableRelayServers)
temp += item.Value.RoomCount;
return temp;
}
}
}