mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-14 01:42:55 +00:00
Added RetryAfter property for ratelimit errors, added parsing of rate limit return
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace CryptoExchange.Net.Objects
|
||||
using System;
|
||||
|
||||
namespace CryptoExchange.Net.Objects
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for errors
|
||||
@@ -202,15 +204,14 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rate limit exceeded
|
||||
/// Rate limit exceeded (client side)
|
||||
/// </summary>
|
||||
public class RateLimitError : Error
|
||||
public abstract class BaseRateLimitError : Error
|
||||
{
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// When the request can be retried
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public RateLimitError(string message) : base(null, "Rate limit exceeded: " + message, null) { }
|
||||
public DateTime? RetryAfter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
@@ -218,7 +219,47 @@
|
||||
/// <param name="code"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="data"></param>
|
||||
protected RateLimitError(int? code, string message, object? data): base(code, message, data) { }
|
||||
protected BaseRateLimitError(int? code, string message, object? data) : base(code, message, data) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rate limit exceeded (client side)
|
||||
/// </summary>
|
||||
public class ClientRateLimitError : BaseRateLimitError
|
||||
{
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public ClientRateLimitError(string message) : base(null, "Client rate limit exceeded: " + message, null) { }
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="data"></param>
|
||||
protected ClientRateLimitError(int? code, string message, object? data): base(code, message, data) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rate limit exceeded (server side)
|
||||
/// </summary>
|
||||
public class ServerRateLimitError : BaseRateLimitError
|
||||
{
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public ServerRateLimitError(string message) : base(null, "Server rate limit exceeded: " + message, null) { }
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="data"></param>
|
||||
protected ServerRateLimitError(int? code, string message, object? data) : base(code, message, data) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -256,7 +256,7 @@ namespace CryptoExchange.Net.Objects
|
||||
historyTopic.Semaphore.Release();
|
||||
var msg = $"Request to {endpoint} failed because of rate limit `{historyTopic.Type}`. Current weight: {currentWeight}/{historyTopic.Limit}, request weight: {requestWeight}";
|
||||
logger.Log(LogLevel.Warning, msg);
|
||||
return new CallResult<int>(new RateLimitError(msg));
|
||||
return new CallResult<int>(new ClientRateLimitError(msg) { RetryAfter = DateTime.UtcNow.AddSeconds(thisWaitTime) });
|
||||
}
|
||||
|
||||
logger.Log(LogLevel.Information, $"Request to {endpoint} waiting {thisWaitTime}ms for rate limit `{historyTopic.Type}`. Current weight: {currentWeight}/{historyTopic.Limit}, request weight: {requestWeight}");
|
||||
|
||||
Reference in New Issue
Block a user