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.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>


+ 7
- 5
src/Discord.Net.Rest/DiscordRestApiClient.cs 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);


+ 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) { }

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)
{


+ 2
- 2
src/Discord.Net.WebSocket/BaseSocketClient.cs 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.


+ 2
- 1
src/Discord.Net.WebSocket/DiscordShardedClient.cs 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)
{


+ 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,
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)


+ 2
- 1
src/Discord.Net.WebSocket/DiscordSocketClient.cs 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)
{


+ 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);
}
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,


Loading…
Cancel
Save