Browse Source

Add default ratelimit callback (#2037)

tags/3.2.0
Quin Lynch GitHub 3 years ago
parent
commit
4d9389b3aa
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 13 deletions
  1. +10
    -0
      src/Discord.Net.Core/DiscordConfig.cs
  2. +7
    -5
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  3. +1
    -1
      src/Discord.Net.Rest/DiscordRestClient.cs
  4. +2
    -2
      src/Discord.Net.WebSocket/BaseSocketClient.cs
  5. +2
    -1
      src/Discord.Net.WebSocket/DiscordShardedClient.cs
  6. +2
    -2
      src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
  7. +2
    -1
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  8. +1
    -1
      src/Discord.Net.Webhook/DiscordWebhookClient.cs

+ 10
- 0
src/Discord.Net.Core/DiscordConfig.cs View File

@@ -1,4 +1,6 @@
using System;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks;


namespace Discord namespace Discord
{ {
@@ -131,6 +133,14 @@ namespace Discord
/// </returns> /// </returns>
public RetryMode DefaultRetryMode { get; set; } = RetryMode.AlwaysRetry; public RetryMode DefaultRetryMode { get; set; } = RetryMode.AlwaysRetry;


/// <summary>
/// Gets or sets the default callback for ratelimits.
/// </summary>
/// <remarks>
/// This property is mutually exclusive with <see cref="RequestOptions.RatelimitCallback"/>.
/// </remarks>
public Func<IRateLimitInfo, Task> DefaultRatelimitCallback { get; set; }

/// <summary> /// <summary>
/// Gets or sets the minimum log level severity that will be sent to the Log event. /// Gets or sets the minimum log level severity that will be sent to the Log event.
/// </summary> /// </summary>


+ 7
- 5
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -46,17 +46,19 @@ namespace Discord.API
internal IRestClient RestClient { get; private set; } internal IRestClient RestClient { get; private set; }
internal ulong? CurrentUserId { get; set; } internal ulong? CurrentUserId { get; set; }
internal bool UseSystemClock { get; set; } internal bool UseSystemClock { get; set; }
internal Func<IRateLimitInfo, Task> DefaultRatelimitCallback { get; set; }
internal JsonSerializer Serializer => _serializer; internal JsonSerializer Serializer => _serializer;


/// <exception cref="ArgumentException">Unknown OAuth token type.</exception> /// <exception cref="ArgumentException">Unknown OAuth token type.</exception>
public DiscordRestApiClient(RestClientProvider restClientProvider, string userAgent, RetryMode defaultRetryMode = RetryMode.AlwaysRetry, public DiscordRestApiClient(RestClientProvider restClientProvider, string userAgent, RetryMode defaultRetryMode = RetryMode.AlwaysRetry,
JsonSerializer serializer = null, bool useSystemClock = true)
JsonSerializer serializer = null, bool useSystemClock = true, Func<IRateLimitInfo, Task> defaultRatelimitCallback = null)
{ {
_restClientProvider = restClientProvider; _restClientProvider = restClientProvider;
UserAgent = userAgent; UserAgent = userAgent;
DefaultRetryMode = defaultRetryMode; DefaultRetryMode = defaultRetryMode;
_serializer = serializer ?? new JsonSerializer { ContractResolver = new DiscordContractResolver() }; _serializer = serializer ?? new JsonSerializer { ContractResolver = new DiscordContractResolver() };
UseSystemClock = useSystemClock; UseSystemClock = useSystemClock;
DefaultRatelimitCallback = defaultRatelimitCallback;


RequestQueue = new RequestQueue(); RequestQueue = new RequestQueue();
_stateLock = new SemaphoreSlim(1, 1); _stateLock = new SemaphoreSlim(1, 1);
@@ -279,10 +281,10 @@ namespace Discord.API
{ {
if (!request.Options.IgnoreState) if (!request.Options.IgnoreState)
CheckState(); CheckState();
if (request.Options.RetryMode == null)
request.Options.RetryMode = DefaultRetryMode;
if (request.Options.UseSystemClock == null)
request.Options.UseSystemClock = UseSystemClock;
request.Options.RetryMode ??= DefaultRetryMode;
request.Options.UseSystemClock ??= UseSystemClock;
request.Options.RatelimitCallback ??= DefaultRatelimitCallback;


var stopwatch = Stopwatch.StartNew(); var stopwatch = Stopwatch.StartNew();
var responseStream = await RequestQueue.SendAsync(request).ConfigureAwait(false); var responseStream = await RequestQueue.SendAsync(request).ConfigureAwait(false);


+ 1
- 1
src/Discord.Net.Rest/DiscordRestClient.cs View File

@@ -37,7 +37,7 @@ namespace Discord.Rest
internal DiscordRestClient(DiscordRestConfig config, API.DiscordRestApiClient api) : base(config, api) { } internal DiscordRestClient(DiscordRestConfig config, API.DiscordRestApiClient api) : base(config, api) { }


private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config)
=> new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, serializer: Serializer, useSystemClock: config.UseSystemClock);
=> new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, serializer: Serializer, useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback);


internal override void Dispose(bool disposing) internal override void Dispose(bool disposing)
{ {


+ 2
- 2
src/Discord.Net.WebSocket/BaseSocketClient.cs View File

@@ -79,8 +79,8 @@ namespace Discord.WebSocket
internal BaseSocketClient(DiscordSocketConfig config, DiscordRestApiClient client) internal BaseSocketClient(DiscordSocketConfig config, DiscordRestApiClient client)
: base(config, client) => BaseConfig = config; : base(config, client) => BaseConfig = config;
private static DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config) private static DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config)
=> new DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent,
useSystemClock: config.UseSystemClock);
=> new DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent, config.GatewayHost,
useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback);


/// <summary> /// <summary>
/// Gets a Discord application information for the logged-in user. /// Gets a Discord application information for the logged-in user.


+ 2
- 1
src/Discord.Net.WebSocket/DiscordShardedClient.cs View File

@@ -107,7 +107,8 @@ namespace Discord.WebSocket
} }
} }
private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config) private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config)
=> new API.DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent);
=> new DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent, config.GatewayHost,
useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback);


internal async Task AcquireIdentifyLockAsync(int shardId, CancellationToken token) internal async Task AcquireIdentifyLockAsync(int shardId, CancellationToken token)
{ {


+ 2
- 2
src/Discord.Net.WebSocket/DiscordSocketApiClient.cs View File

@@ -39,8 +39,8 @@ namespace Discord.API


public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider, string userAgent, public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider, string userAgent,
string url = null, RetryMode defaultRetryMode = RetryMode.AlwaysRetry, JsonSerializer serializer = null, string url = null, RetryMode defaultRetryMode = RetryMode.AlwaysRetry, JsonSerializer serializer = null,
bool useSystemClock = true)
: base(restClientProvider, userAgent, defaultRetryMode, serializer, useSystemClock)
bool useSystemClock = true, Func<IRateLimitInfo, Task> defaultRatelimitCallback = null)
: base(restClientProvider, userAgent, defaultRetryMode, serializer, useSystemClock, defaultRatelimitCallback)
{ {
_gatewayUrl = url; _gatewayUrl = url;
if (url != null) if (url != null)


+ 2
- 1
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -195,7 +195,8 @@ namespace Discord.WebSocket
_largeGuilds = new ConcurrentQueue<ulong>(); _largeGuilds = new ConcurrentQueue<ulong>();
} }
private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config) private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config)
=> new API.DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent, config.GatewayHost);
=> new DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent, config.GatewayHost,
useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback);
/// <inheritdoc /> /// <inheritdoc />
internal override void Dispose(bool disposing) internal override void Dispose(bool disposing)
{ {


+ 1
- 1
src/Discord.Net.Webhook/DiscordWebhookClient.cs View File

@@ -83,7 +83,7 @@ namespace Discord.Webhook
ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false); ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false);
} }
private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config)
=> new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent);
=> new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback);
/// <summary> Sends a message to the channel for this webhook. </summary> /// <summary> Sends a message to the channel for this webhook. </summary>
/// <returns> Returns the ID of the created message. </returns> /// <returns> Returns the ID of the created message. </returns>
public Task<ulong> SendMessageAsync(string text = null, bool isTTS = false, IEnumerable<Embed> embeds = null, public Task<ulong> SendMessageAsync(string text = null, bool isTTS = false, IEnumerable<Embed> embeds = null,


Loading…
Cancel
Save