Add default ratelimit callback (#2037)

This commit is contained in:
Quin Lynch
2022-01-14 07:55:57 -04:00
committed by GitHub
parent 3429cf99c8
commit 4d9389b3aa
8 changed files with 27 additions and 13 deletions

View File

@@ -1,4 +1,6 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace Discord
{
@@ -131,6 +133,14 @@ namespace Discord
/// </returns>
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>
/// Gets or sets the minimum log level severity that will be sent to the Log event.
/// </summary>

View File

@@ -46,17 +46,19 @@ namespace Discord.API
internal IRestClient RestClient { get; private set; }
internal ulong? CurrentUserId { get; set; }
internal bool UseSystemClock { get; set; }
internal Func<IRateLimitInfo, Task> DefaultRatelimitCallback { get; set; }
internal JsonSerializer Serializer => _serializer;
/// <exception cref="ArgumentException">Unknown OAuth token type.</exception>
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;
UserAgent = userAgent;
DefaultRetryMode = defaultRetryMode;
_serializer = serializer ?? new JsonSerializer { ContractResolver = new DiscordContractResolver() };
UseSystemClock = useSystemClock;
DefaultRatelimitCallback = defaultRatelimitCallback;
RequestQueue = new RequestQueue();
_stateLock = new SemaphoreSlim(1, 1);
@@ -279,10 +281,10 @@ namespace Discord.API
{
if (!request.Options.IgnoreState)
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 responseStream = await RequestQueue.SendAsync(request).ConfigureAwait(false);

View File

@@ -37,7 +37,7 @@ namespace Discord.Rest
internal DiscordRestClient(DiscordRestConfig config, API.DiscordRestApiClient api) : base(config, api) { }
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)
{

View File

@@ -79,8 +79,8 @@ namespace Discord.WebSocket
internal BaseSocketClient(DiscordSocketConfig config, DiscordRestApiClient client)
: base(config, client) => BaseConfig = 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>
/// Gets a Discord application information for the logged-in user.

View File

@@ -107,7 +107,8 @@ namespace Discord.WebSocket
}
}
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)
{

View File

@@ -39,8 +39,8 @@ namespace Discord.API
public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider, string userAgent,
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;
if (url != null)

View File

@@ -195,7 +195,8 @@ namespace Discord.WebSocket
_largeGuilds = new ConcurrentQueue<ulong>();
}
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 />
internal override void Dispose(bool disposing)
{

View File

@@ -83,7 +83,7 @@ namespace Discord.Webhook
ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false);
}
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>
/// <returns> Returns the ID of the created message. </returns>
public Task<ulong> SendMessageAsync(string text = null, bool isTTS = false, IEnumerable<Embed> embeds = null,