Add periodic garbage collection of webhook rate limit cache

This commit is contained in:
Ske
2020-04-16 18:18:08 +02:00
parent 93e53be7db
commit 93bf7c6543
2 changed files with 28 additions and 2 deletions

View File

@@ -87,6 +87,23 @@ namespace PluralKit.Bot
}
}
public void GarbageCollect()
{
_logger.Information("Garbage-collecting webhook rate limit buckets...");
var collected = 0;
foreach (var channel in _info.Keys)
{
if (!_info.TryGetValue(channel, out var info)) continue;
// Remove all keys that expired more than an hour ago (and of course, haven't been reset)
if (info.resetTime < SystemClock.Instance.GetCurrentInstant() - Duration.FromHours(1))
if (_info.TryRemove(channel, out _)) collected++;
}
_logger.Information("Garbage-collected {ChannelCount} channels from the webhook rate limit buckets.", collected);
}
private string GetHeader(HttpResponseMessage response, string key)
{
var firstPair = response.Headers.FirstOrDefault(pair => pair.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));