Added stats to load balancer
This commit is contained in:
parent
63de178659
commit
de2229bcde
4 changed files with 100 additions and 35 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,17 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
[RestResource]
|
||||
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>
|
||||
/// Sent from an LRM server node
|
||||
/// adds it to the list if authenticated.
|
||||
|
|
@ -136,6 +147,17 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
else
|
||||
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
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace LightReflectiveMirror.LoadBalancing
|
||||
{
|
||||
class Program
|
||||
partial class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Keeps track of all available relays.
|
||||
|
|
@ -18,6 +18,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
|
||||
private int _pingDelay = 10000;
|
||||
public static bool showDebugLogs = false;
|
||||
public static DateTime startupTime;
|
||||
const string API_PATH = "/api/stats";
|
||||
readonly string CONFIG_PATH = System.Environment.GetEnvironmentVariable("LRM_LB_CONFIG_PATH") ?? "config.json";
|
||||
|
||||
|
|
@ -30,6 +31,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
{
|
||||
WriteTitle();
|
||||
instance = this;
|
||||
startupTime = DateTime.Now;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue