Skip to content

Commit

Permalink
fix: Implement correct ratelimit handles for 429's (#2110)
Browse files Browse the repository at this point in the history
* init

* fix errors
  • Loading branch information
quinchs authored Feb 16, 2022
1 parent abfba3c commit b2598d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
16 changes: 13 additions & 3 deletions src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<Stream> SendAsync(RestRequest request)
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] (!) 429");
#endif
UpdateRateLimit(id, request, info, true);
UpdateRateLimit(id, request, info, true, body:response.Stream);
}
await _queue.RaiseRateLimitTriggered(Id, info, $"{request.Method} {request.Endpoint}").ConfigureAwait(false);
continue; //Retry
Expand Down Expand Up @@ -316,7 +316,7 @@ private async Task EnterAsync(int id, IRequest request)
}
}

private void UpdateRateLimit(int id, IRequest request, RateLimitInfo info, bool is429, bool redirected = false)
private void UpdateRateLimit(int id, IRequest request, RateLimitInfo info, bool is429, bool redirected = false, Stream body = null)
{
if (WindowCount == 0)
return;
Expand Down Expand Up @@ -373,7 +373,17 @@ private void UpdateRateLimit(int id, IRequest request, RateLimitInfo info, bool
Debug.WriteLine($"[{id}] X-RateLimit-Remaining: " + info.Remaining.Value);
_semaphore = info.Remaining.Value;
}*/
if (info.RetryAfter.HasValue)
if (is429)
{
// use the payload reset after value
var payload = info.ReadRatelimitPayload(body);

resetTick = DateTimeOffset.UtcNow.Add(TimeSpan.FromSeconds(payload.RetryAfter));
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Reset-After: {info.ResetAfter.Value} ({info.ResetAfter?.TotalMilliseconds} ms)");
#endif
}
else if (info.RetryAfter.HasValue)
{
//RetryAfter is more accurate than Reset, where available
resetTick = DateTimeOffset.UtcNow.AddSeconds(info.RetryAfter.Value);
Expand Down
18 changes: 7 additions & 11 deletions src/Discord.Net.Rest/Net/RateLimitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,18 @@ internal RateLimitInfo(Dictionary<string, string> headers, string endpoint)
DateTimeOffset.TryParse(temp, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
}

internal void ReadRatelimitPayload(Stream response)
internal Ratelimit ReadRatelimitPayload(Stream response)
{
try
if (response != null && response.Length != 0)
{
if (response != null && response.Length != 0)
using (TextReader text = new StreamReader(response))
using (JsonReader reader = new JsonTextReader(text))
{
using (TextReader text = new StreamReader(response))
using (JsonReader reader = new JsonTextReader(text))
{
var ratelimit = Discord.Rest.DiscordRestClient.Serializer.Deserialize<Ratelimit>(reader);

ResetAfter = TimeSpan.FromSeconds(ratelimit.RetryAfter);
}
return Discord.Rest.DiscordRestClient.Serializer.Deserialize<Ratelimit>(reader);
}
}
catch { }

return null;
}
}
}

0 comments on commit b2598d3

Please sign in to comment.