mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-07 07:56:12 +00:00
Fix for ratelimiting possibly creating negative waits
This commit is contained in:
parent
ff0550b0fb
commit
be68115099
@ -80,7 +80,10 @@ namespace CryptoExchange.Net.RateLimiting.Trackers
|
||||
private TimeSpan DetermineWaitTime(int requestWeight)
|
||||
{
|
||||
var weightToRemove = Math.Max(Current - (Limit - requestWeight), 0);
|
||||
return TimeSpan.FromMilliseconds(Math.Ceiling(weightToRemove / DecreaseRate) * TimePeriod.TotalMilliseconds);
|
||||
var result = TimeSpan.FromMilliseconds(Math.Ceiling(weightToRemove / DecreaseRate) * TimePeriod.TotalMilliseconds);
|
||||
if (result < TimeSpan.Zero)
|
||||
return TimeSpan.Zero;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -97,7 +97,10 @@ namespace CryptoExchange.Net.RateLimiting.Trackers
|
||||
private TimeSpan DetermineWaitTime()
|
||||
{
|
||||
var checkTime = DateTime.UtcNow;
|
||||
return (_nextReset!.Value - checkTime) + _fixedWindowBuffer;
|
||||
var result = (_nextReset!.Value - checkTime) + _fixedWindowBuffer;
|
||||
if (result < TimeSpan.Zero)
|
||||
return TimeSpan.Zero;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +93,10 @@ namespace CryptoExchange.Net.RateLimiting.Trackers
|
||||
var checkTime = DateTime.UtcNow;
|
||||
var startCurrentWindow = checkTime.AddTicks(-(checkTime.Ticks % TimePeriod.Ticks));
|
||||
var wait = startCurrentWindow.Add(TimePeriod) - checkTime;
|
||||
return wait.Add(_fixedWindowBuffer);
|
||||
var result = wait.Add(_fixedWindowBuffer);
|
||||
if (result < TimeSpan.Zero)
|
||||
return TimeSpan.Zero;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,11 @@ namespace CryptoExchange.Net.RateLimiting.Trackers
|
||||
private readonly List<LimitEntry> _entries;
|
||||
private int _currentWeight = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Additional wait time to apply to account for fluctuating request times
|
||||
/// </summary>
|
||||
private static readonly TimeSpan _slidingWindowBuffer = TimeSpan.FromMilliseconds(1000);
|
||||
|
||||
public SlidingWindowTracker(int limit, TimeSpan period)
|
||||
{
|
||||
Limit = limit;
|
||||
@ -89,7 +94,10 @@ namespace CryptoExchange.Net.RateLimiting.Trackers
|
||||
removedWeight += entry.Weight;
|
||||
if (removedWeight >= weightToRemove)
|
||||
{
|
||||
return entry.Timestamp + TimePeriod - DateTime.UtcNow;
|
||||
var result = entry.Timestamp + TimePeriod + _slidingWindowBuffer - DateTime.UtcNow;
|
||||
if (result < TimeSpan.Zero)
|
||||
return TimeSpan.Zero;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,6 +80,10 @@ namespace CryptoExchange.Net.Testing.Comparers
|
||||
else if (jObj.Type == JTokenType.Array)
|
||||
{
|
||||
var resultObj = enumerator.Current;
|
||||
if (resultObj is string)
|
||||
// string list
|
||||
continue;
|
||||
|
||||
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
|
||||
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
|
||||
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
|
||||
|
@ -82,6 +82,10 @@ namespace CryptoExchange.Net.Testing.Comparers
|
||||
else if (jObj.Type == JTokenType.Array)
|
||||
{
|
||||
var resultObj = enumerator.Current;
|
||||
if (resultObj is string)
|
||||
// string list
|
||||
continue;
|
||||
|
||||
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
|
||||
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
|
||||
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
|
||||
|
Loading…
x
Reference in New Issue
Block a user