From c7023b38dbccaee0a440c05879a9627f8ebe06bf Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sun, 10 Dec 2023 22:39:34 +0200 Subject: [PATCH] Don't respect rate limits optimistically --- .../Discord/DiscordClient.cs | 36 +------------------ 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/DiscordChatExporter.Core/Discord/DiscordClient.cs b/DiscordChatExporter.Core/Discord/DiscordClient.cs index 5ad9862f..05e907fd 100644 --- a/DiscordChatExporter.Core/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Core/Discord/DiscordClient.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Net; using System.Net.Http; @@ -43,44 +42,11 @@ public class DiscordClient(string token) tokenKind == TokenKind.Bot ? $"Bot {token}" : token ); - var response = await Http.Client.SendAsync( + return await Http.Client.SendAsync( request, HttpCompletionOption.ResponseHeadersRead, innerCancellationToken ); - - // If this was the last request available before hitting the rate limit, - // wait out the reset time so that future requests can succeed. - // This may add an unnecessary delay in case the user doesn't intend to - // make any more requests, but implementing a smarter solution would - // require properly keeping track of Discord's global/per-route/per-resource - // rate limits and that's just way too much effort. - // https://discord.com/developers/docs/topics/rate-limits - var remainingRequestCount = response - .Headers - .TryGetValue("X-RateLimit-Remaining") - ?.Pipe(s => int.Parse(s, CultureInfo.InvariantCulture)); - - var resetAfterDelay = response - .Headers - .TryGetValue("X-RateLimit-Reset-After") - ?.Pipe(s => double.Parse(s, CultureInfo.InvariantCulture)) - .Pipe(TimeSpan.FromSeconds); - - if (remainingRequestCount <= 0 && resetAfterDelay is not null) - { - var delay = - // Adding a small buffer to the reset time reduces the chance of getting - // rate limited again, because it allows for more requests to be released. - (resetAfterDelay.Value + TimeSpan.FromSeconds(1)) - // Sometimes Discord returns an absurdly high value for the reset time, which - // is not actually enforced by the server. So we cap it at a reasonable value. - .Clamp(TimeSpan.Zero, TimeSpan.FromSeconds(60)); - - await Task.Delay(delay, innerCancellationToken); - } - - return response; }, cancellationToken );