using CryptoExchange.Net.Objects;
using CryptoExchange.Net.RateLimiting.Interfaces;
using System;
namespace CryptoExchange.Net.RateLimiting.Guards
{
///
/// Retry after guard
///
public class RetryAfterGuard : IRateLimitGuard
{
///
/// Additional wait time to apply to account for time offset between server and client
///
private static readonly TimeSpan _windowBuffer = TimeSpan.FromMilliseconds(1000);
///
public string Name => "RetryAfterGuard";
///
public string Description => $"Pause {Type} until after {After}";
///
/// The timestamp after which requests are allowed again
///
public DateTime After { get; private set; }
///
/// The type of rate limit item this guard is for
///
public RateLimitItemType Type { get; private set; }
///
/// ctor
///
///
///
public RetryAfterGuard(DateTime after, RateLimitItemType type)
{
After = after;
Type = type;
}
///
public LimitCheck Check(RateLimitItemType type, RequestDefinition definition, string? apiKey, int requestWeight, string? keySuffix, double allowedRateRatio)
{
if (type != Type)
return LimitCheck.NotApplicable;
var dif = (After + _windowBuffer) - DateTime.UtcNow;
if (dif <= TimeSpan.Zero)
return LimitCheck.NotApplicable;
return LimitCheck.Needed(dif, default, default, default);
}
///
public RateLimitState ApplyWeight(RateLimitItemType type, RequestDefinition definition, string? apiKey, int requestWeight, string? keySuffix)
{
return RateLimitState.NotApplied;
}
///
/// Update the 'after' time
///
///
public void UpdateAfter(DateTime after) => After = after;
///
public void Reset(RateLimitItemType type, RequestDefinition definition, string? apiKey, string? keySuffix, int? amount)
{
After = DateTime.UtcNow;
}
}
}