Add default ratelimit callback (#2037)
This commit is contained in:
@@ -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>
|
||||||
|
|||||||
@@ -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;
|
request.Options.RetryMode ??= DefaultRetryMode;
|
||||||
if (request.Options.UseSystemClock == null)
|
request.Options.UseSystemClock ??= UseSystemClock;
|
||||||
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);
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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,
|
=> new DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent, config.GatewayHost,
|
||||||
useSystemClock: config.UseSystemClock);
|
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.
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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)
|
bool useSystemClock = true, Func<IRateLimitInfo, Task> defaultRatelimitCallback = null)
|
||||||
: base(restClientProvider, userAgent, defaultRetryMode, serializer, useSystemClock)
|
: base(restClientProvider, userAgent, defaultRetryMode, serializer, useSystemClock, defaultRatelimitCallback)
|
||||||
{
|
{
|
||||||
_gatewayUrl = url;
|
_gatewayUrl = url;
|
||||||
if (url != null)
|
if (url != null)
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user