Server list Endpoint Caching (Thanks FakeByte! :))

This commit is contained in:
Derek S 2021-04-05 23:30:35 -05:00
parent 046cfe3d4f
commit 35bd75bc05
5 changed files with 29 additions and 13 deletions

View file

@ -48,7 +48,7 @@ namespace LightReflectiveMirror.LoadBalancing
WriteLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red);
}
var pingThread = new Thread(new ThreadStart(() => PingServers()));
var pingThread = new Thread(new ThreadStart(PingServers));
pingThread.Start();
// keep console alive
@ -99,7 +99,7 @@ namespace LightReflectiveMirror.LoadBalancing
}
}
async Task PingServers()
async void PingServers()
{
while (true)
{

View file

@ -23,7 +23,9 @@ namespace LightReflectiveMirror.Endpoints
[RestResource]
public class Endpoint
{
private List<Room> _rooms { get => Program.instance.GetRooms().Where(x => x.isPublic).ToList(); }
private static string _cachedServerList = "[]";
private static string _cachedCompressedServerList;
private static List<Room> _rooms { get => Program.instance.GetRooms().Where(x => x.isPublic).ToList(); }
private RelayStats _stats { get => new RelayStats
{
@ -33,6 +35,12 @@ namespace LightReflectiveMirror.Endpoints
Uptime = Program.instance.GetUptime()
}; }
public static void RoomsModified()
{
_cachedServerList = JsonConvert.SerializeObject(_rooms, Formatting.Indented);
_cachedCompressedServerList = _cachedServerList.Compress();
}
[RestRoute("Get", "/api/stats")]
public async Task Stats(IHttpContext context)
{
@ -45,8 +53,7 @@ namespace LightReflectiveMirror.Endpoints
{
if (Program.conf.EndpointServerList)
{
string json = JsonConvert.SerializeObject(_rooms, Formatting.Indented);
await context.Response.SendResponseAsync(json);
await context.Response.SendResponseAsync(_cachedServerList);
}
else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
@ -57,8 +64,7 @@ namespace LightReflectiveMirror.Endpoints
{
if (Program.conf.EndpointServerList)
{
string json = JsonConvert.SerializeObject(_rooms);
await context.Response.SendResponseAsync(json.Compress());
await context.Response.SendResponseAsync(_cachedCompressedServerList);
}
else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);

View file

@ -110,11 +110,12 @@ namespace LightReflectiveMirror
if (conf.UseEndpoint)
{
WriteLogMessage("\nStarting Endpoint Service... ", ConsoleColor.White, true);
var endpoint = new EndpointServer();
var endpointService = new EndpointServer();
if (endpoint.Start(conf.EndpointPort))
if (endpointService.Start(conf.EndpointPort))
{
WriteLogMessage("OK", ConsoleColor.Green);
Endpoint.RoomsModified();
}
else
{

View file

@ -1,4 +1,5 @@
using Mirror;
using LightReflectiveMirror.Endpoints;
using Mirror;
using System;
using System.Collections.Generic;
using System.Net;

View file

@ -1,4 +1,5 @@
using System;
using LightReflectiveMirror.Endpoints;
using System;
using System.Collections.Generic;
using System.Net;
@ -75,6 +76,7 @@ namespace LightReflectiveMirror
_sendBuffers.Return(sendJoinBuffer);
Endpoint.RoomsModified();
return;
}
else
@ -86,6 +88,8 @@ namespace LightReflectiveMirror
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
_sendBuffers.Return(sendJoinBuffer);
Endpoint.RoomsModified();
return;
}
}
@ -151,6 +155,8 @@ namespace LightReflectiveMirror
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
_sendBuffers.Return(sendBuffer);
Endpoint.RoomsModified();
}
/// <summary>
@ -174,6 +180,7 @@ namespace LightReflectiveMirror
_sendBuffers.Return(sendBuffer);
rooms[i].clients.Clear();
rooms.RemoveAt(i);
Endpoint.RoomsModified();
return;
}
else
@ -191,6 +198,7 @@ namespace LightReflectiveMirror
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
_sendBuffers.Return(sendBuffer);
Endpoint.RoomsModified();
}
}
}