|
@@ -1,4 +1,5 @@ |
|
|
using Newtonsoft.Json; |
|
|
|
|
|
|
|
|
using Discord.Net.Rest; |
|
|
|
|
|
using Newtonsoft.Json; |
|
|
using Newtonsoft.Json.Linq; |
|
|
using Newtonsoft.Json.Linq; |
|
|
using System; |
|
|
using System; |
|
|
#if DEBUG_LIMITS |
|
|
#if DEBUG_LIMITS |
|
@@ -54,64 +55,79 @@ namespace Discord.Net.Queue |
|
|
#if DEBUG_LIMITS |
|
|
#if DEBUG_LIMITS |
|
|
Debug.WriteLine($"[{id}] Sending..."); |
|
|
Debug.WriteLine($"[{id}] Sending..."); |
|
|
#endif |
|
|
#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 |
|
|
#if DEBUG_LIMITS |
|
|
Debug.WriteLine($"[{id}] (!) 429 [Global]"); |
|
|
|
|
|
|
|
|
Debug.WriteLine($"[{id}] (!) 429 [Global]"); |
|
|
#endif |
|
|
#endif |
|
|
_queue.PauseGlobal(info, lag); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
_queue.PauseGlobal(info, lag); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
#if DEBUG_LIMITS |
|
|
#if DEBUG_LIMITS |
|
|
Debug.WriteLine($"[{id}] (!) 429"); |
|
|
|
|
|
|
|
|
Debug.WriteLine($"[{id}] (!) 429"); |
|
|
#endif |
|
|
#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 |
|
|
#if DEBUG_LIMITS |
|
|
Debug.WriteLine($"[{id}] (!) 502"); |
|
|
|
|
|
|
|
|
Debug.WriteLine($"[{id}] (!) 502"); |
|
|
#endif |
|
|
#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 |
|
|
#if DEBUG_LIMITS |
|
|
Debug.WriteLine($"[{id}] Success"); |
|
|
|
|
|
|
|
|
catch |
|
|
|
|
|
{ |
|
|
|
|
|
Debug.WriteLine($"[{id}] Error"); |
|
|
|
|
|
throw; |
|
|
|
|
|
} |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
finally |
|
|
|
|
|
{ |
|
|
UpdateRateLimit(id, request, info, lag, false); |
|
|
UpdateRateLimit(id, request, info, lag, false); |
|
|
#if DEBUG_LIMITS |
|
|
#if DEBUG_LIMITS |
|
|
Debug.WriteLine($"[{id}] Stop"); |
|
|
Debug.WriteLine($"[{id}] Stop"); |
|
|
#endif |
|
|
#endif |
|
|
return response.Stream; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|