Browse Source

Ensure rate limits are updated when requests fail

tags/1.0-rc
RogueException 8 years ago
parent
commit
228209aec8
1 changed files with 55 additions and 39 deletions
  1. +55
    -39
      src/Discord.Net.Core/Net/Queue/RequestQueueBucket.cs

+ 55
- 39
src/Discord.Net.Core/Net/Queue/RequestQueueBucket.cs View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Discord.Net.Rest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
#if DEBUG_LIMITS
@@ -54,64 +55,79 @@ namespace Discord.Net.Queue
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Sending...");
#endif
var response = await request.SendAsync().ConfigureAwait(false);
TimeSpan lag = DateTimeOffset.UtcNow - DateTimeOffset.Parse(response.Headers["Date"]);
var info = new RateLimitInfo(response.Headers);

if (response.StatusCode < (HttpStatusCode)200 || response.StatusCode >= (HttpStatusCode)300)
TimeSpan lag = default(TimeSpan);
RateLimitInfo info = default(RateLimitInfo);
try
{
switch (response.StatusCode)
var response = await request.SendAsync().ConfigureAwait(false);
lag = DateTimeOffset.UtcNow - DateTimeOffset.Parse(response.Headers["Date"]);
info = new RateLimitInfo(response.Headers);

if (response.StatusCode < (HttpStatusCode)200 || response.StatusCode >= (HttpStatusCode)300)
{
case (HttpStatusCode)429:
if (info.IsGlobal)
{
switch (response.StatusCode)
{
case (HttpStatusCode)429:
if (info.IsGlobal)
{
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] (!) 429 [Global]");
Debug.WriteLine($"[{id}] (!) 429 [Global]");
#endif
_queue.PauseGlobal(info, lag);
}
else
{
_queue.PauseGlobal(info, lag);
}
else
{
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] (!) 429");
Debug.WriteLine($"[{id}] (!) 429");
#endif
UpdateRateLimit(id, request, info, lag, true);
}
await _queue.RaiseRateLimitTriggered(Id, info).ConfigureAwait(false);
continue; //Retry
case HttpStatusCode.BadGateway: //502
UpdateRateLimit(id, request, info, lag, true);
}
await _queue.RaiseRateLimitTriggered(Id, info).ConfigureAwait(false);
continue; //Retry
case HttpStatusCode.BadGateway: //502
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] (!) 502");
Debug.WriteLine($"[{id}] (!) 502");
#endif
continue; //Continue
default:
string reason = null;
if (response.Stream != null)
{
try
continue; //Continue
default:
string reason = null;
if (response.Stream != null)
{
using (var reader = new StreamReader(response.Stream))
using (var jsonReader = new JsonTextReader(reader))
try
{
var json = JToken.Load(jsonReader);
reason = json.Value<string>("message");
using (var reader = new StreamReader(response.Stream))
using (var jsonReader = new JsonTextReader(reader))
{
var json = JToken.Load(jsonReader);
reason = json.Value<string>("message");
}
}
catch { }
}
catch { }
}
throw new HttpException(response.StatusCode, reason);
throw new HttpException(response.StatusCode, reason);
}
}
else
{
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Success");
#endif
return response.Stream;
}
}
else
{
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Success");
catch
{
Debug.WriteLine($"[{id}] Error");
throw;
}
#endif
finally
{
UpdateRateLimit(id, request, info, lag, false);
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Stop");
#endif
return response.Stream;
}
}
}


Loading…
Cancel
Save