Browse Source

Disabled rate limit debugging by default

tags/1.0-rc
RogueException 8 years ago
parent
commit
2e95e4232f
2 changed files with 38 additions and 0 deletions
  1. +2
    -0
      src/Discord.Net.Core/Net/Queue/RequestQueue.cs
  2. +36
    -0
      src/Discord.Net.Core/Net/Queue/RequestQueueBucket.cs

+ 2
- 0
src/Discord.Net.Core/Net/Queue/RequestQueue.cs View File

@@ -79,7 +79,9 @@ namespace Discord.Net.Queue
int millis = (int)Math.Ceiling((_waitUntil - DateTimeOffset.UtcNow).TotalMilliseconds); int millis = (int)Math.Ceiling((_waitUntil - DateTimeOffset.UtcNow).TotalMilliseconds);
if (millis > 0) if (millis > 0)
{ {
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Sleeping {millis} ms (Pre-emptive) [Global]"); Debug.WriteLine($"[{id}] Sleeping {millis} ms (Pre-emptive) [Global]");
#endif
await Task.Delay(millis).ConfigureAwait(false); await Task.Delay(millis).ConfigureAwait(false);
} }
} }


+ 36
- 0
src/Discord.Net.Core/Net/Queue/RequestQueueBucket.cs View File

@@ -1,7 +1,9 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
#if DEBUG_LIMITS
using System.Diagnostics; using System.Diagnostics;
#endif
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
@@ -40,14 +42,18 @@ namespace Discord.Net.Queue
public async Task<Stream> SendAsync(RestRequest request) public async Task<Stream> SendAsync(RestRequest request)
{ {
int id = Interlocked.Increment(ref nextId); int id = Interlocked.Increment(ref nextId);
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Start"); Debug.WriteLine($"[{id}] Start");
#endif
LastAttemptAt = DateTimeOffset.UtcNow; LastAttemptAt = DateTimeOffset.UtcNow;
while (true) while (true)
{ {
await _queue.EnterGlobalAsync(id, request).ConfigureAwait(false); await _queue.EnterGlobalAsync(id, request).ConfigureAwait(false);
await EnterAsync(id, request).ConfigureAwait(false); await EnterAsync(id, request).ConfigureAwait(false);


#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Sending..."); Debug.WriteLine($"[{id}] Sending...");
#endif
var response = await request.SendAsync().ConfigureAwait(false); var response = await request.SendAsync().ConfigureAwait(false);
TimeSpan lag = DateTimeOffset.UtcNow - DateTimeOffset.Parse(response.Headers["Date"]); TimeSpan lag = DateTimeOffset.UtcNow - DateTimeOffset.Parse(response.Headers["Date"]);
var info = new RateLimitInfo(response.Headers); var info = new RateLimitInfo(response.Headers);
@@ -59,18 +65,24 @@ namespace Discord.Net.Queue
case (HttpStatusCode)429: case (HttpStatusCode)429:
if (info.IsGlobal) if (info.IsGlobal)
{ {
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] (!) 429 [Global]"); Debug.WriteLine($"[{id}] (!) 429 [Global]");
#endif
_queue.PauseGlobal(info, lag); _queue.PauseGlobal(info, lag);
} }
else else
{ {
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] (!) 429"); Debug.WriteLine($"[{id}] (!) 429");
#endif
UpdateRateLimit(id, request, info, lag, true); UpdateRateLimit(id, request, info, lag, true);
} }
await _queue.RaiseRateLimitTriggered(Id, info).ConfigureAwait(false); await _queue.RaiseRateLimitTriggered(Id, info).ConfigureAwait(false);
continue; //Retry continue; //Retry
case HttpStatusCode.BadGateway: //502 case HttpStatusCode.BadGateway: //502
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] (!) 502"); Debug.WriteLine($"[{id}] (!) 502");
#endif
continue; //Continue continue; //Continue
default: default:
string reason = null; string reason = null;
@@ -92,9 +104,13 @@ namespace Discord.Net.Queue
} }
else else
{ {
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Success"); Debug.WriteLine($"[{id}] Success");
#endif
UpdateRateLimit(id, request, info, lag, false); UpdateRateLimit(id, request, info, lag, false);
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Stop"); Debug.WriteLine($"[{id}] Stop");
#endif
return response.Stream; return response.Stream;
} }
} }
@@ -135,7 +151,9 @@ namespace Discord.Net.Queue
if (resetAt > timeoutAt) if (resetAt > timeoutAt)
throw new RateLimitedException(); throw new RateLimitedException();
int millis = (int)Math.Ceiling((resetAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds); int millis = (int)Math.Ceiling((resetAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds);
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Sleeping {millis} ms (Pre-emptive)"); Debug.WriteLine($"[{id}] Sleeping {millis} ms (Pre-emptive)");
#endif
if (millis > 0) if (millis > 0)
await Task.Delay(millis, request.CancelToken).ConfigureAwait(false); await Task.Delay(millis, request.CancelToken).ConfigureAwait(false);
} }
@@ -143,13 +161,17 @@ namespace Discord.Net.Queue
{ {
if ((timeoutAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds < 500.0) if ((timeoutAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds < 500.0)
throw new RateLimitedException(); throw new RateLimitedException();
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Sleeping 500* ms (Pre-emptive)"); Debug.WriteLine($"[{id}] Sleeping 500* ms (Pre-emptive)");
#endif
await Task.Delay(500, request.CancelToken).ConfigureAwait(false); await Task.Delay(500, request.CancelToken).ConfigureAwait(false);
} }
continue; continue;
} }
#if DEBUG_LIMITS
else else
Debug.WriteLine($"[{id}] Entered Semaphore ({_semaphore}/{WindowCount} remaining)"); Debug.WriteLine($"[{id}] Entered Semaphore ({_semaphore}/{WindowCount} remaining)");
#endif
break; break;
} }
} }
@@ -166,7 +188,9 @@ namespace Discord.Net.Queue
{ {
WindowCount = info.Limit.Value; WindowCount = info.Limit.Value;
_semaphore = info.Remaining.Value; _semaphore = info.Remaining.Value;
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Upgraded Semaphore to {info.Remaining.Value}/{WindowCount}"); Debug.WriteLine($"[{id}] Upgraded Semaphore to {info.Remaining.Value}/{WindowCount}");
#endif
} }


var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
@@ -182,24 +206,32 @@ namespace Discord.Net.Queue
{ {
//RetryAfter is more accurate than Reset, where available //RetryAfter is more accurate than Reset, where available
resetTick = DateTimeOffset.UtcNow.AddMilliseconds(info.RetryAfter.Value); resetTick = DateTimeOffset.UtcNow.AddMilliseconds(info.RetryAfter.Value);
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Retry-After: {info.RetryAfter.Value} ({info.RetryAfter.Value} ms)"); Debug.WriteLine($"[{id}] Retry-After: {info.RetryAfter.Value} ({info.RetryAfter.Value} ms)");
#endif
} }
else if (info.Reset.HasValue) else if (info.Reset.HasValue)
{ {
resetTick = info.Reset.Value.AddSeconds(/*1.0 +*/ lag.TotalSeconds); resetTick = info.Reset.Value.AddSeconds(/*1.0 +*/ lag.TotalSeconds);
int diff = (int)(resetTick.Value - DateTimeOffset.UtcNow).TotalMilliseconds; int diff = (int)(resetTick.Value - DateTimeOffset.UtcNow).TotalMilliseconds;
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] X-RateLimit-Reset: {info.Reset.Value.ToUnixTimeSeconds()} ({diff} ms, {lag.TotalMilliseconds} ms lag)"); Debug.WriteLine($"[{id}] X-RateLimit-Reset: {info.Reset.Value.ToUnixTimeSeconds()} ({diff} ms, {lag.TotalMilliseconds} ms lag)");
#endif
} }
else if (request.Options.ClientBucketId != null) else if (request.Options.ClientBucketId != null)
{ {
resetTick = DateTimeOffset.UtcNow.AddSeconds(ClientBucket.Get(request.Options.ClientBucketId).WindowSeconds); resetTick = DateTimeOffset.UtcNow.AddSeconds(ClientBucket.Get(request.Options.ClientBucketId).WindowSeconds);
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Client Bucket ({ClientBucket.Get(request.Options.ClientBucketId).WindowSeconds * 1000} ms)"); Debug.WriteLine($"[{id}] Client Bucket ({ClientBucket.Get(request.Options.ClientBucketId).WindowSeconds * 1000} ms)");
#endif
} }


if (resetTick == null) if (resetTick == null)
{ {
WindowCount = 0; //No rate limit info, disable limits on this bucket (should only ever happen with a user token) WindowCount = 0; //No rate limit info, disable limits on this bucket (should only ever happen with a user token)
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Disabled Semaphore"); Debug.WriteLine($"[{id}] Disabled Semaphore");
#endif
return; return;
} }


@@ -207,7 +239,9 @@ namespace Discord.Net.Queue
{ {
_resetTick = resetTick; _resetTick = resetTick;
LastAttemptAt = resetTick.Value; //Make sure we dont destroy this until after its been reset LastAttemptAt = resetTick.Value; //Make sure we dont destroy this until after its been reset
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Reset in {(int)Math.Ceiling((resetTick - DateTimeOffset.UtcNow).Value.TotalMilliseconds)} ms"); Debug.WriteLine($"[{id}] Reset in {(int)Math.Ceiling((resetTick - DateTimeOffset.UtcNow).Value.TotalMilliseconds)} ms");
#endif


if (!hasQueuedReset) if (!hasQueuedReset)
{ {
@@ -227,7 +261,9 @@ namespace Discord.Net.Queue
millis = (int)Math.Ceiling((_resetTick.Value - DateTimeOffset.UtcNow).TotalMilliseconds); millis = (int)Math.Ceiling((_resetTick.Value - DateTimeOffset.UtcNow).TotalMilliseconds);
if (millis <= 0) //Make sure we havent gotten a more accurate reset time if (millis <= 0) //Make sure we havent gotten a more accurate reset time
{ {
#if DEBUG_LIMITS
Debug.WriteLine($"[{id}] * Reset *"); Debug.WriteLine($"[{id}] * Reset *");
#endif
_semaphore = WindowCount; _semaphore = WindowCount;
_resetTick = null; _resetTick = null;
return; return;


Loading…
Cancel
Save