Added rate limit warning to log.
This commit is contained in:
@@ -21,10 +21,10 @@ namespace Discord
|
||||
internal RestClient RestClient => _rest;
|
||||
private readonly RestClient _rest;
|
||||
|
||||
public DiscordAPIClient(DiscordConfig config = null)
|
||||
public DiscordAPIClient(DiscordConfig config = null, Logger logger = null)
|
||||
{
|
||||
_config = config ?? new DiscordConfig();
|
||||
_rest = new RestClient(_config);
|
||||
_rest = new RestClient(_config, logger);
|
||||
}
|
||||
|
||||
private string _token;
|
||||
|
||||
@@ -14,17 +14,17 @@ namespace Discord.Net.Rest
|
||||
private readonly IRestEngine _engine;
|
||||
private CancellationToken _cancelToken;
|
||||
|
||||
public RestClient(DiscordConfig config)
|
||||
public RestClient(DiscordConfig config, Logger logger)
|
||||
{
|
||||
_config = config;
|
||||
#if !DOTNET5_4
|
||||
_engine = new RestSharpEngine(config);
|
||||
_engine = new RestSharpEngine(config, logger);
|
||||
#else
|
||||
//_engine = new BuiltInRestEngine(config);
|
||||
//_engine = new BuiltInRestEngine(config, logger);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public void SetToken(string token) => _engine.SetToken(token);
|
||||
public void SetToken(string token) => _engine.SetToken(token);
|
||||
|
||||
//DELETE
|
||||
internal Task<ResponseT> Delete<ResponseT>(string path, object data) where ResponseT : class
|
||||
|
||||
@@ -11,13 +11,19 @@ namespace Discord.Net.Rest
|
||||
{
|
||||
internal sealed class RestSharpEngine : IRestEngine
|
||||
{
|
||||
private readonly DiscordConfig _config;
|
||||
private readonly DiscordConfig _config;
|
||||
private readonly RestSharp.RestClient _client;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RestSharpEngine(DiscordConfig config)
|
||||
private readonly object _rateLimitLock;
|
||||
private DateTime _rateLimitTime;
|
||||
|
||||
public RestSharpEngine(DiscordConfig config, Logger logger)
|
||||
{
|
||||
_config = config;
|
||||
_client = new RestSharp.RestClient(Endpoints.BaseApi)
|
||||
_logger = logger;
|
||||
_rateLimitLock = new object();
|
||||
_client = new RestSharp.RestClient(Endpoints.BaseApi)
|
||||
{
|
||||
PreAuthenticate = false,
|
||||
ReadWriteTimeout = _config.RestTimeout,
|
||||
@@ -58,8 +64,8 @@ namespace Discord.Net.Rest
|
||||
return Send(request, cancelToken);
|
||||
}
|
||||
private async Task<string> Send(RestRequest request, CancellationToken cancelToken)
|
||||
{
|
||||
int retryCount = 0;
|
||||
{
|
||||
int retryCount = 0;
|
||||
while (true)
|
||||
{
|
||||
var response = await _client.ExecuteTaskAsync(request, cancelToken).ConfigureAwait(false);
|
||||
@@ -78,7 +84,22 @@ namespace Discord.Net.Rest
|
||||
int milliseconds;
|
||||
if (retryAfter != null && int.TryParse((string)retryAfter.Value, out milliseconds))
|
||||
{
|
||||
await Task.Delay(milliseconds).ConfigureAwait(false);
|
||||
if (_logger != null)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
if (now >= _rateLimitTime)
|
||||
{
|
||||
lock (_rateLimitLock)
|
||||
{
|
||||
if (now >= _rateLimitTime)
|
||||
{
|
||||
_rateLimitTime = now.AddMilliseconds(milliseconds);
|
||||
_logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
await Task.Delay(milliseconds, cancelToken).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
throw new HttpException(response.StatusCode);
|
||||
|
||||
Reference in New Issue
Block a user