Fix admin logs cache caching rounds forever if multiple game servers are ran on the same db (#30687)

This commit is contained in:
DrSmugleaf
2024-08-05 16:13:27 -07:00
committed by GitHub
parent 9bb9e80ede
commit edd17072ae

View File

@@ -16,6 +16,7 @@ public sealed partial class AdminLogManager
// TODO ADMIN LOGS make this thread safe or remove thread safety from the main partial class
private readonly Dictionary<int, List<SharedAdminLog>> _roundsLogCache = new(MaxRoundsCached);
private readonly Queue<int> _roundsLogCacheQueue = new();
private static readonly Gauge CacheRoundCount = Metrics.CreateGauge(
"admin_logs_cache_round_count",
@@ -28,19 +29,21 @@ public sealed partial class AdminLogManager
// TODO ADMIN LOGS cache previous {MaxRoundsCached} rounds on startup
public void CacheNewRound()
{
List<SharedAdminLog> list;
var oldestRound = _currentRoundId - MaxRoundsCached;
List<SharedAdminLog>? list = null;
if (_roundsLogCache.Remove(oldestRound, out var oldestList))
_roundsLogCacheQueue.Enqueue(_currentRoundId);
if (_roundsLogCacheQueue.Count > MaxRoundsCached)
{
list = oldestList;
list.Clear();
}
else
{
list = new List<SharedAdminLog>(LogListInitialSize);
var oldestRound = _roundsLogCacheQueue.Dequeue();
if (_roundsLogCache.Remove(oldestRound, out var oldestList))
{
list = oldestList;
list.Clear();
}
}
list ??= new List<SharedAdminLog>(LogListInitialSize);
_roundsLogCache.Add(_currentRoundId, list);
CacheRoundCount.Set(_roundsLogCache.Count);
}