Smarter rate limit handling

This commit is contained in:
Tyrrrz
2022-12-15 19:55:50 +02:00
parent 57e2dc9313
commit 49c4b12512
3 changed files with 28 additions and 16 deletions

View File

@@ -4,7 +4,7 @@ namespace DiscordChatExporter.Core.Utils.Extensions;
public static class HttpExtensions
{
public static string? TryGetValue(this HttpContentHeaders headers, string name) =>
public static string? TryGetValue(this HttpHeaders headers, string name) =>
headers.TryGetValues(name, out var values)
? string.Concat(values)
: null;

View File

@@ -39,19 +39,9 @@ public static class Http
8,
(i, result, _) =>
{
// If rate-limited, use retry-after as a guide
if (result.Result?.StatusCode == HttpStatusCode.TooManyRequests)
{
// Only start respecting retry-after after a few attempts, because
// Discord often sends unreasonable (20+ minutes) retry-after
// on the very first request.
if (i > 3)
{
var retryAfterDelay = result.Result.Headers.RetryAfter?.Delta;
if (retryAfterDelay is not null)
return retryAfterDelay.Value + TimeSpan.FromSeconds(1); // margin just in case
}
}
// If rate-limited, use retry-after header as the guide
if (result.Result.Headers.RetryAfter?.Delta is { } retryAfter)
return retryAfter + TimeSpan.FromSeconds(1);
return TimeSpan.FromSeconds(Math.Pow(2, i) + 1);
},