Added rate limit support
This commit is contained in:
@@ -3,6 +3,7 @@ using Discord.API;
|
|||||||
using RestSharp;
|
using RestSharp;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -63,15 +64,29 @@ namespace Discord.Net.Rest
|
|||||||
{
|
{
|
||||||
var response = await _client.ExecuteTaskAsync(request, cancelToken).ConfigureAwait(false);
|
var response = await _client.ExecuteTaskAsync(request, cancelToken).ConfigureAwait(false);
|
||||||
int statusCode = (int)response.StatusCode;
|
int statusCode = (int)response.StatusCode;
|
||||||
if (statusCode == 0) //Internal Error
|
if (statusCode == 0) //Internal Error
|
||||||
{
|
{
|
||||||
if (response.ErrorException.HResult == -2146233079 && retryCount++ < 5) //The request was aborted: Could not create SSL/TLS secure channel.
|
//The request was aborted: Could not create SSL/TLS secure channel.
|
||||||
continue; //Seems to work if we immediately retry
|
if (response.ErrorException.HResult == -2146233079 && retryCount++ < 5)
|
||||||
throw response.ErrorException;
|
continue; //Retrying seems to fix this somehow?
|
||||||
}
|
throw response.ErrorException;
|
||||||
if (statusCode < 200 || statusCode >= 300) //2xx = Success
|
}
|
||||||
|
else if (statusCode == 429) //Rate limit
|
||||||
|
{
|
||||||
|
var retryAfter = response.Headers
|
||||||
|
.FirstOrDefault(x => x.Name.Equals("Retry-After", StringComparison.OrdinalIgnoreCase));
|
||||||
|
int milliseconds;
|
||||||
|
if (retryAfter != null)
|
||||||
|
{
|
||||||
|
await Task.Delay((int)retryAfter.Value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
throw new HttpException(response.StatusCode);
|
||||||
|
}
|
||||||
|
else if (statusCode < 200 || statusCode >= 300) //2xx = Success
|
||||||
throw new HttpException(response.StatusCode);
|
throw new HttpException(response.StatusCode);
|
||||||
return response.Content;
|
else
|
||||||
|
return response.Content;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user