From e716d5d482c9848ec02578659ed337df331f0d75 Mon Sep 17 00:00:00 2001 From: adamelfstrom <105274564+adamelfstrom@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:31:20 +0100 Subject: [PATCH 1/2] Make rate limit test more lenient --- FortnoxSDK.Tests/RateLimiterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FortnoxSDK.Tests/RateLimiterTests.cs b/FortnoxSDK.Tests/RateLimiterTests.cs index 634345cd..736b7833 100644 --- a/FortnoxSDK.Tests/RateLimiterTests.cs +++ b/FortnoxSDK.Tests/RateLimiterTests.cs @@ -39,7 +39,7 @@ public async Task Test_RateLimiter_NoError() * 200 requests should be executed in ~50 seconds. */ Assert.AreEqual(200, counter); - Assert.IsTrue(watch.Elapsed.TotalSeconds is > 49 and < 51); + Assert.IsTrue(watch.Elapsed.TotalSeconds is > 48 and < 52); } [Ignore("Can make other test fail due to exhausting rate limiter")] From 727e8f542abaaba24576b026184b5d239f91d55d Mon Sep 17 00:00:00 2001 From: adamelfstrom <105274564+adamelfstrom@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:55:23 +0100 Subject: [PATCH 2/2] Fix spelling of Throttle and increase rate limiter test maximum interval to 65s --- FortnoxSDK.Tests/RateLimiterTests.cs | 3 ++- FortnoxSDK/Connectors/Base/BaseClient.cs | 2 +- FortnoxSDK/Connectors/Base/RateLimiter.cs | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/FortnoxSDK.Tests/RateLimiterTests.cs b/FortnoxSDK.Tests/RateLimiterTests.cs index 736b7833..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 > 48 and < 52); + 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;