mirror of
https://github.com/myawired/2025-RecRoom-Server-E12354.git
synced 2026-07-18 00:21:15 +10:00
Initial upload
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
public static class RateLimit
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, DateTime> _requests = new();
|
||||
|
||||
public static bool TryConsume(
|
||||
string key,
|
||||
int cooldownSeconds,
|
||||
out int retryAfterSeconds)
|
||||
{
|
||||
retryAfterSeconds = 0;
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
var last = _requests.GetOrAdd(key, _ => DateTime.MinValue);
|
||||
var elapsed = (now - last).TotalSeconds;
|
||||
|
||||
if (elapsed < cooldownSeconds)
|
||||
{
|
||||
retryAfterSeconds = (int)Math.Ceiling(cooldownSeconds - elapsed);
|
||||
return false;
|
||||
}
|
||||
|
||||
_requests[key] = now;
|
||||
|
||||
Cleanup(now);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void Cleanup(DateTime now)
|
||||
{
|
||||
if (_requests.Count < 10_000) return;
|
||||
|
||||
foreach (var kv in _requests)
|
||||
{
|
||||
if ((now - kv.Value).TotalMinutes > 10)
|
||||
_requests.TryRemove(kv.Key, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user