Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/Discord.Net.Rest/Net/DefaultRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ public DefaultRestClient(string baseUrl, bool useProxy = false, IWebProxy webPro
_baseUrl = baseUrl;

#pragma warning disable IDISP014
_client = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
UseCookies = false,
UseProxy = useProxy,
Proxy = webProxy
});
var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, UseCookies = false, UseProxy = useProxy };
if (useProxy) handler.Proxy = webProxy;
_client = new HttpClient(handler);
#pragma warning restore IDISP014
SetHeader("accept-encoding", "gzip, deflate");

Expand Down
6 changes: 3 additions & 3 deletions src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task<Stream> SendAsync(RestRequest request)
}*/
finally
{
UpdateRateLimit(id, request, info, response.StatusCode == (HttpStatusCode)429, body: response.Stream);
UpdateRateLimit(id, request, info, response.StatusCode == (HttpStatusCode)429, restResponse: response);
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Stop");
#endif
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, Stream body = null)
private void UpdateRateLimit(int id, IRequest request, RateLimitInfo info, bool is429, bool redirected = false, RestResponse restResponse = default)
{
if (WindowCount == 0)
return;
Expand Down Expand Up @@ -386,7 +386,7 @@ private void UpdateRateLimit(int id, IRequest request, RateLimitInfo info, bool
_semaphore = 0;

// use the payload reset after value
var payload = info.ReadRatelimitPayload(body);
var payload = info.ReadRatelimitPayload(restResponse);

// fallback on stored ratelimit info when payload is null, https://github.com/discord-net/Discord.Net/issues/2123
resetTick = DateTimeOffset.UtcNow.Add(TimeSpan.FromSeconds(payload?.RetryAfter ?? info.ResetAfter?.TotalSeconds ?? 0));
Expand Down
21 changes: 19 additions & 2 deletions src/Discord.Net.Rest/Net/RateLimitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,27 @@ internal RateLimitInfo(Dictionary<string, string> headers, string endpoint)
DateTimeOffset.TryParse(temp, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
}

internal Ratelimit ReadRatelimitPayload(Stream response)
internal Ratelimit ReadRatelimitPayload(RestResponse restResponse)
{
if (response != null && response.Length != 0)
var response = restResponse.Stream;
if (response != null && response.Length != 0 && response.CanRead)
{
var pos = response.Position;
using (TextReader text = new StreamReader(response))
{
if (text.ReadToEnd().StartsWith("<"))
{
return new Ratelimit
{
Global = true,
Message = "Cloudflare HTML limit",
RetryAfter = restResponse.Headers.TryGetValue("Retry-After", out var retry) ? float.Parse(retry) : 60 * 30 // 30 mins
};
}
}

response.Position = pos;

using (TextReader text = new StreamReader(response))
using (JsonReader reader = new JsonTextReader(text))
{
Expand Down