PluralKit/Myriad/Rest/Ratelimit/Ratelimiter.cs

86 lines
2.8 KiB
C#
Raw Normal View History

2020-12-22 12:15:26 +00:00
using Myriad.Rest.Exceptions;
using Serilog;
namespace Myriad.Rest.Ratelimit;
public class Ratelimiter: IDisposable
2020-12-22 12:15:26 +00:00
{
private readonly BucketManager _buckets;
private readonly ILogger _logger;
private DateTimeOffset? _globalRateLimitExpiry;
public Ratelimiter(ILogger logger)
2020-12-22 12:15:26 +00:00
{
_logger = logger.ForContext<Ratelimiter>();
_buckets = new BucketManager(logger);
}
2020-12-22 12:15:26 +00:00
public void Dispose()
{
_buckets.Dispose();
}
2020-12-22 12:15:26 +00:00
public void AllowRequestOrThrow(string endpoint, ulong major, DateTimeOffset now)
{
if (IsGloballyRateLimited(now))
2020-12-22 12:15:26 +00:00
{
_logger.Warning("Globally rate limited until {GlobalRateLimitExpiry}, cancelling request",
_globalRateLimitExpiry);
throw new GloballyRatelimitedException();
2020-12-22 12:15:26 +00:00
}
var bucket = _buckets.GetBucket(endpoint, major);
if (bucket == null)
2020-12-22 12:15:26 +00:00
{
// No rate limit for this endpoint (yet), allow through
_logger.Debug("No rate limit data for endpoint {Endpoint}, allowing through", endpoint);
return;
2020-12-22 12:15:26 +00:00
}
bucket.Tick(now);
2020-12-22 12:15:26 +00:00
if (bucket.TryAcquire())
// We're allowed to send it! :)
return;
2020-12-22 12:15:26 +00:00
// We can't send this request right now; retrying...
var waitTime = bucket.GetResetDelay(now);
2020-12-22 12:15:26 +00:00
// add a small buffer for Timing:tm:
waitTime += TimeSpan.FromMilliseconds(50);
2020-12-22 12:15:26 +00:00
// (this is caught by a WaitAndRetry Polly handler, if configured)
throw new RatelimitBucketExhaustedException(bucket, waitTime);
}
2020-12-22 12:15:26 +00:00
public void HandleResponse(RatelimitHeaders headers, string endpoint, ulong major)
{
if (!headers.HasRatelimitInfo)
return;
2020-12-22 12:15:26 +00:00
if (endpoint.Contains("interactions"))
_logger.Information($"Discord debug: got rate limit headers for interaction endpoint: global? {headers.Global}, limit: {headers.Limit}, remaining: {headers.Remaining}, reset: {headers.Reset?.ToUnixTimeSeconds()}, reset_after: {headers.ResetAfter?.TotalSeconds}, bucket: {headers.Bucket}");
// TODO: properly calculate server time?
if (headers.Global)
2020-12-22 12:15:26 +00:00
{
_logger.Warning(
"Global rate limit hit, resetting at {GlobalRateLimitExpiry} (in {GlobalRateLimitResetAfter}!",
_globalRateLimitExpiry, headers.ResetAfter);
_globalRateLimitExpiry = headers.Reset;
2020-12-22 12:15:26 +00:00
}
else
{
// Update buckets first, then get it again, to properly "transfer" this info over to the new value
_buckets.UpdateEndpointInfo(endpoint, headers.Bucket!, headers.Limit);
2020-12-22 12:15:26 +00:00
var bucket = _buckets.GetBucket(endpoint, major);
bucket?.HandleResponse(headers);
}
2020-12-22 12:15:26 +00:00
}
private bool IsGloballyRateLimited(DateTimeOffset now) =>
_globalRateLimitExpiry > now;
2020-12-22 12:15:26 +00:00
}