Garbage collection setup that might be needed later
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Hangfire.Core" Version="1.8.12" />
|
||||
<PackageReference Include="Hangfire.MemoryStorage" Version="1.8.0" />
|
||||
<PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
|
||||
13
Program.cs
13
Program.cs
@@ -1,3 +1,5 @@
|
||||
using Hangfire;
|
||||
using Hangfire.MemoryStorage;
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -9,6 +11,14 @@ services.AddControllers();
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen();
|
||||
services.AddScoped<IRoomManager, RoomManager>();
|
||||
// var roomManager = services.BuildServiceProvider().GetRequiredService<IRoomManager>();
|
||||
//
|
||||
// services.AddHangfire((sp, config) =>
|
||||
// {
|
||||
// config.UseRecommendedSerializerSettings();
|
||||
// config.UseMemoryStorage();
|
||||
// });
|
||||
// services.AddHangfireServer();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -26,6 +36,9 @@ var webSocketOptions = new WebSocketOptions()
|
||||
|
||||
app.UseWebSockets(webSocketOptions);
|
||||
|
||||
// var recurringJobManager = app.Services.GetRequiredService<IRecurringJobManagerV2>();
|
||||
// recurringJobManager.AddOrUpdate("1", () => roomManager.ClearGhostRooms(), Cron.Hourly);
|
||||
|
||||
app.UseCors(policyBuilder =>
|
||||
{
|
||||
policyBuilder.AllowAnyOrigin();
|
||||
|
||||
@@ -99,7 +99,7 @@ public class Room
|
||||
return _connections.Select(connection => connection.Socket).ToList();
|
||||
}
|
||||
|
||||
private bool IsEmpty()
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return _connections.Count == 0;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,16 @@ public class RoomManager : IRoomManager
|
||||
{
|
||||
return Rooms.ContainsKey(roomId);
|
||||
}
|
||||
|
||||
public void ClearGhostRooms()
|
||||
{
|
||||
var ghostRooms = Rooms.Where(room => room.Value.IsEmpty());
|
||||
foreach (var room in ghostRooms)
|
||||
{
|
||||
GC.Collect(GC.GetGeneration(room.Value));
|
||||
RemoveRoom(room.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IRoomManager
|
||||
@@ -40,4 +50,5 @@ public interface IRoomManager
|
||||
public List<string> GetAllRooms();
|
||||
public void RemoveRoom(string roomId);
|
||||
public bool RoomExists(string roomId);
|
||||
public void ClearGhostRooms();
|
||||
}
|
||||
Reference in New Issue
Block a user