1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2026-04-07 18:21:30 +00:00

Fixed concurrency issue when using rate limit guard for multiple gates

This commit is contained in:
Jkorf 2026-04-07 09:57:47 +02:00
parent cdd0bd83ab
commit 93034e8af8

View File

@ -3,6 +3,7 @@ using CryptoExchange.Net.RateLimiting.Interfaces;
using CryptoExchange.Net.RateLimiting.Trackers; using CryptoExchange.Net.RateLimiting.Trackers;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
namespace CryptoExchange.Net.RateLimiting.Guards namespace CryptoExchange.Net.RateLimiting.Guards
{ {
@ -36,6 +37,7 @@ namespace CryptoExchange.Net.RateLimiting.Guards
private readonly double? _decayRate; private readonly double? _decayRate;
private readonly int? _connectionWeight; private readonly int? _connectionWeight;
private readonly Func<RequestDefinition, string, string?, string> _keySelector; private readonly Func<RequestDefinition, string, string?, string> _keySelector;
private readonly SemaphoreSlim? _sharedGuardSemaphore;
/// <inheritdoc /> /// <inheritdoc />
public string Name => "RateLimitGuard"; public string Name => "RateLimitGuard";
@ -52,6 +54,11 @@ namespace CryptoExchange.Net.RateLimiting.Guards
/// </summary> /// </summary>
public TimeSpan TimeSpan { get; } public TimeSpan TimeSpan { get; }
/// <summary>
/// Whether this guard is shared between multiple gates
/// </summary>
public bool SharedGuard { get; }
/// <summary> /// <summary>
/// ctor /// ctor
/// </summary> /// </summary>
@ -62,8 +69,9 @@ namespace CryptoExchange.Net.RateLimiting.Guards
/// <param name="windowType">Type of rate limit window</param> /// <param name="windowType">Type of rate limit window</param>
/// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param> /// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param>
/// <param name="connectionWeight">The weight of a new connection</param> /// <param name="connectionWeight">The weight of a new connection</param>
public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IGuardFilter filter, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null) /// <param name="shared">Whether this guard is shared between multiple gates</param>
: this(keySelector, new[] { filter }, limit, timeSpan, windowType, decayPerTimeSpan, connectionWeight) public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IGuardFilter filter, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null, bool shared = false)
: this(keySelector, new[] { filter }, limit, timeSpan, windowType, decayPerTimeSpan, connectionWeight, shared)
{ {
} }
@ -77,16 +85,21 @@ namespace CryptoExchange.Net.RateLimiting.Guards
/// <param name="windowType">Type of rate limit window</param> /// <param name="windowType">Type of rate limit window</param>
/// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param> /// <param name="decayPerTimeSpan">The decay per timespan if windowType is DecayWindowTracker</param>
/// <param name="connectionWeight">The weight of a new connection</param> /// <param name="connectionWeight">The weight of a new connection</param>
public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IEnumerable<IGuardFilter> filters, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null) /// <param name="shared">Whether this guard is shared between multiple gates</param>
public RateLimitGuard(Func<RequestDefinition, string, string?, string> keySelector, IEnumerable<IGuardFilter> filters, int limit, TimeSpan timeSpan, RateLimitWindowType windowType, double? decayPerTimeSpan = null, int? connectionWeight = null, bool shared = false)
{ {
_filters = filters; _filters = filters;
_trackers = new Dictionary<string, IWindowTracker>(); _trackers = new Dictionary<string, IWindowTracker>();
_windowType = windowType; _windowType = windowType;
Limit = limit; Limit = limit;
TimeSpan = timeSpan; TimeSpan = timeSpan;
SharedGuard = shared;
_keySelector = keySelector; _keySelector = keySelector;
_decayRate = decayPerTimeSpan; _decayRate = decayPerTimeSpan;
_connectionWeight = connectionWeight; _connectionWeight = connectionWeight;
if (SharedGuard)
_sharedGuardSemaphore = new SemaphoreSlim(1, 1);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -101,6 +114,11 @@ namespace CryptoExchange.Net.RateLimiting.Guards
if (type == RateLimitItemType.Connection) if (type == RateLimitItemType.Connection)
requestWeight = _connectionWeight ?? requestWeight; requestWeight = _connectionWeight ?? requestWeight;
if (SharedGuard)
_sharedGuardSemaphore!.Wait();
try
{
var key = _keySelector(definition, host, apiKey) + keySuffix; var key = _keySelector(definition, host, apiKey) + keySuffix;
if (!_trackers.TryGetValue(key, out var tracker)) if (!_trackers.TryGetValue(key, out var tracker))
{ {
@ -108,12 +126,19 @@ namespace CryptoExchange.Net.RateLimiting.Guards
_trackers.Add(key, tracker); _trackers.Add(key, tracker);
} }
var delay = tracker.GetWaitTime(requestWeight); var delay = tracker.GetWaitTime(requestWeight);
if (delay == default) if (delay == default)
return LimitCheck.NotNeeded(Limit, TimeSpan, tracker.Current); return LimitCheck.NotNeeded(Limit, TimeSpan, tracker.Current);
return LimitCheck.Needed(delay, Limit, TimeSpan, tracker.Current); return LimitCheck.Needed(delay, Limit, TimeSpan, tracker.Current);
} }
finally
{
if (SharedGuard)
_sharedGuardSemaphore!.Release();
}
}
/// <inheritdoc /> /// <inheritdoc />
public RateLimitState ApplyWeight(RateLimitItemType type, RequestDefinition definition, string host, string? apiKey, int requestWeight, string? keySuffix) public RateLimitState ApplyWeight(RateLimitItemType type, RequestDefinition definition, string host, string? apiKey, int requestWeight, string? keySuffix)
@ -127,9 +152,23 @@ namespace CryptoExchange.Net.RateLimiting.Guards
if (type == RateLimitItemType.Connection) if (type == RateLimitItemType.Connection)
requestWeight = _connectionWeight ?? requestWeight; requestWeight = _connectionWeight ?? requestWeight;
var key = _keySelector(definition, host, apiKey) + keySuffix; var key = _keySelector(definition, host, apiKey) + keySuffix;
var tracker = _trackers[key]; var tracker = _trackers[key];
if (SharedGuard)
_sharedGuardSemaphore!.Wait();
try
{
tracker.ApplyWeight(requestWeight); tracker.ApplyWeight(requestWeight);
}
finally
{
if (SharedGuard)
_sharedGuardSemaphore!.Release();
}
return RateLimitState.Applied(Limit, TimeSpan, tracker.Current); return RateLimitState.Applied(Limit, TimeSpan, tracker.Current);
} }