1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Fixed exception being thrown when waiting was canceled during rate limiting

This commit is contained in:
JKorf 2024-08-28 19:10:08 +02:00
parent 42003a0247
commit b1cd9b5412
2 changed files with 19 additions and 4 deletions

View File

@ -370,5 +370,20 @@ namespace CryptoExchange.Net.UnitTests
var result2 = await rateLimiter.ProcessAsync(new TraceLogger(), 1, RateLimitItemType.Connection, new RequestDefinition("1", HttpMethod.Get), host2, "123", 1, RateLimitingBehaviour.Wait, default);
Assert.That(expectLimited ? evnt != null : evnt == null);
}
[Test]
public async Task ConnectionRateLimiterCancel()
{
var rateLimiter = new RateLimitGate("Test");
rateLimiter.AddGuard(new RateLimitGuard(RateLimitGuard.PerHost, new LimitItemTypeFilter(RateLimitItemType.Connection), 1, TimeSpan.FromSeconds(10), RateLimitWindowType.Fixed));
RateLimitEvent evnt = null;
rateLimiter.RateLimitTriggered += (x) => { evnt = x; };
var ct = new CancellationTokenSource(TimeSpan.FromSeconds(0.2));
var result1 = await rateLimiter.ProcessAsync(new TraceLogger(), 1, RateLimitItemType.Connection, new RequestDefinition("1", HttpMethod.Get), "https://test.com", "123", 1, RateLimitingBehaviour.Wait, ct.Token);
var result2 = await rateLimiter.ProcessAsync(new TraceLogger(), 1, RateLimitItemType.Connection, new RequestDefinition("1", HttpMethod.Get), "https://test.com", "123", 1, RateLimitingBehaviour.Wait, ct.Token);
Assert.That(result2.Error, Is.TypeOf<CancellationRequestedError>());
}
}
}

View File

@ -47,9 +47,9 @@ namespace CryptoExchange.Net.RateLimiting
}
catch (TaskCanceledException)
{
// The semaphore has alraedy been released if the task was cancelled
// The semaphore has already been released if the task was cancelled
release = false;
throw;
return new CallResult(new CancellationRequestedError());
}
finally
{
@ -80,9 +80,9 @@ namespace CryptoExchange.Net.RateLimiting
}
catch (TaskCanceledException)
{
// The semaphore has alraedy been released if the task was cancelled
// The semaphore has already been released if the task was cancelled
release = false;
throw;
return new CallResult(new CancellationRequestedError());
}
finally
{