diff --git a/FortnoxSDK.Tests/RateLimiterTests.cs b/FortnoxSDK.Tests/RateLimiterTests.cs index 634345cd..4b80366e 100644 --- a/FortnoxSDK.Tests/RateLimiterTests.cs +++ b/FortnoxSDK.Tests/RateLimiterTests.cs @@ -37,9 +37,10 @@ public async Task Test_RateLimiter_NoError() /* * Given the rate limiter of 4 requests per second, * 200 requests should be executed in ~50 seconds. + * Note: time per request might be longer than 250ms, hence the longer maximum allowed elapsed time */ Assert.AreEqual(200, counter); - Assert.IsTrue(watch.Elapsed.TotalSeconds is > 49 and < 51); + Assert.IsTrue(watch.Elapsed.TotalSeconds is > 49 and < 65); } [Ignore("Can make other test fail due to exhausting rate limiter")] diff --git a/FortnoxSDK/Connectors/Base/BaseClient.cs b/FortnoxSDK/Connectors/Base/BaseClient.cs index 27e91306..66d9806b 100644 --- a/FortnoxSDK/Connectors/Base/BaseClient.cs +++ b/FortnoxSDK/Connectors/Base/BaseClient.cs @@ -26,7 +26,7 @@ public async Task SendAsync(HttpRequestMessage request) Authorization?.ApplyTo(request); if (UseRateLimiter) - await RateLimiter.Trottle(Authorization?.AccessToken).ConfigureAwait(false); + await RateLimiter.Throttle(Authorization?.AccessToken).ConfigureAwait(false); using var response = await HttpClient.SendAsync(request).ConfigureAwait(false); diff --git a/FortnoxSDK/Connectors/Base/RateLimiter.cs b/FortnoxSDK/Connectors/Base/RateLimiter.cs index 78c9018b..9ed24595 100644 --- a/FortnoxSDK/Connectors/Base/RateLimiter.cs +++ b/FortnoxSDK/Connectors/Base/RateLimiter.cs @@ -10,13 +10,13 @@ namespace Fortnox.SDK.Connectors.Base; /// Rate limit - 4 requests per 1 seconds per token /// internal class RateLimiter -{ +{ private const int MaxCount = 4; private static readonly TimeSpan Period = TimeSpan.FromSeconds(1); - + private static readonly Dictionary RateLimiters = new(); - public async Task Trottle(string token) + public async Task Throttle(string token) { if (token == null) return;