Browse Source

Add request info to RateLimitedException

pull/957/head
Alex Gravely 7 years ago
parent
commit
00dc288a6f
5 changed files with 28 additions and 11 deletions
  1. +14
    -0
      src/Discord.Net.Core/Net/IRequest.cs
  2. +5
    -2
      src/Discord.Net.Core/Net/RateLimitedException.cs
  3. +5
    -5
      src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs
  4. +2
    -2
      src/Discord.Net.Rest/Net/Queue/Requests/RestRequest.cs
  5. +2
    -2
      src/Discord.Net.Rest/Net/Queue/Requests/WebSocketRequest.cs

+ 14
- 0
src/Discord.Net.Core/Net/IRequest.cs View File

@@ -0,0 +1,14 @@
using System;
using System.IO;
using System.Threading.Tasks;

namespace Discord.Net
{
public interface IRequest
{
DateTimeOffset? TimeoutAt { get; }
TaskCompletionSource<Stream> Promise { get; }
RequestOptions Options { get; }
}
}

+ 5
- 2
src/Discord.Net.Core/Net/RateLimitedException.cs View File

@@ -1,12 +1,15 @@
using System;
using System;


namespace Discord.Net namespace Discord.Net
{ {
public class RateLimitedException : TimeoutException public class RateLimitedException : TimeoutException
{ {
public RateLimitedException()
public IRequest Request { get; }

public RateLimitedException(IRequest request)
: base("You are being rate limited.") : base("You are being rate limited.")
{ {
Request = request;
} }
} }
} }

+ 5
- 5
src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
#if DEBUG_LIMITS #if DEBUG_LIMITS
@@ -163,7 +163,7 @@ namespace Discord.Net.Queue
if (!isRateLimited) if (!isRateLimited)
throw new TimeoutException(); throw new TimeoutException();
else else
throw new RateLimitedException();
throw new RateLimitedException(request);
} }


lock (_lock) lock (_lock)
@@ -182,12 +182,12 @@ namespace Discord.Net.Queue
} }


if ((request.Options.RetryMode & RetryMode.RetryRatelimit) == 0) if ((request.Options.RetryMode & RetryMode.RetryRatelimit) == 0)
throw new RateLimitedException();
throw new RateLimitedException(request);


if (resetAt.HasValue) if (resetAt.HasValue)
{ {
if (resetAt > timeoutAt) if (resetAt > timeoutAt)
throw new RateLimitedException();
throw new RateLimitedException(request);
int millis = (int)Math.Ceiling((resetAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds); int millis = (int)Math.Ceiling((resetAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds);
#if DEBUG_LIMITS #if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Sleeping {millis} ms (Pre-emptive)"); Debug.WriteLine($"[{id}] Sleeping {millis} ms (Pre-emptive)");
@@ -198,7 +198,7 @@ namespace Discord.Net.Queue
else else
{ {
if ((timeoutAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds < 500.0) if ((timeoutAt.Value - DateTimeOffset.UtcNow).TotalMilliseconds < 500.0)
throw new RateLimitedException();
throw new RateLimitedException(request);
#if DEBUG_LIMITS #if DEBUG_LIMITS
Debug.WriteLine($"[{id}] Sleeping 500* ms (Pre-emptive)"); Debug.WriteLine($"[{id}] Sleeping 500* ms (Pre-emptive)");
#endif #endif


+ 2
- 2
src/Discord.Net.Rest/Net/Queue/Requests/RestRequest.cs View File

@@ -1,11 +1,11 @@
using Discord.Net.Rest;
using Discord.Net.Rest;
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;


namespace Discord.Net.Queue namespace Discord.Net.Queue
{ {
public class RestRequest
public class RestRequest : IRequest
{ {
public IRestClient Client { get; } public IRestClient Client { get; }
public string Method { get; } public string Method { get; }


+ 2
- 2
src/Discord.Net.Rest/Net/Queue/Requests/WebSocketRequest.cs View File

@@ -1,4 +1,4 @@
using Discord.Net.WebSockets;
using Discord.Net.WebSockets;
using System; using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
@@ -6,7 +6,7 @@ using System.Threading.Tasks;


namespace Discord.Net.Queue namespace Discord.Net.Queue
{ {
public class WebSocketRequest
public class WebSocketRequest : IRequest
{ {
public IWebSocketClient Client { get; } public IWebSocketClient Client { get; }
public string BucketId { get; } public string BucketId { get; }


Loading…
Cancel
Save