diff --git a/FortnoxSDK.Tests/RateLimiterTests.cs b/FortnoxSDK.Tests/RateLimiterTests.cs index 4b80366e..6373d111 100644 --- a/FortnoxSDK.Tests/RateLimiterTests.cs +++ b/FortnoxSDK.Tests/RateLimiterTests.cs @@ -35,12 +35,15 @@ public async Task Test_RateLimiter_NoError() watch.Stop(); /* - * 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 + * Given the rate limiter of 4.8 requests per second (24/5), + * 200 requests should be executed in ~41 seconds. + * + * If we can increase the rate limiter to the advertised 25/5 (5 per second), + * 200 requests should be executed in ~40 seconds. */ + Assert.AreEqual(200, counter); - Assert.IsTrue(watch.Elapsed.TotalSeconds is > 49 and < 65); + Assert.IsTrue(watch.Elapsed.TotalSeconds is > 40); } [Ignore("Can make other test fail due to exhausting rate limiter")] diff --git a/FortnoxSDK/Connectors/Base/RateLimiter.cs b/FortnoxSDK/Connectors/Base/RateLimiter.cs index 9ed24595..912e28e7 100644 --- a/FortnoxSDK/Connectors/Base/RateLimiter.cs +++ b/FortnoxSDK/Connectors/Base/RateLimiter.cs @@ -7,12 +7,12 @@ namespace Fortnox.SDK.Connectors.Base; /// -/// Rate limit - 4 requests per 1 seconds per token +/// Rate limit - 25 requests per 5 seconds per token /// internal class RateLimiter { - private const int MaxCount = 4; - private static readonly TimeSpan Period = TimeSpan.FromSeconds(1); + private const int MaxCount = 24; // Rate limit is 25 requests over 5 second sliding window, but using the max allowed still seems to trigger the limit. 24 req per 5 sec seems to work consistently. + private static readonly TimeSpan Period = TimeSpan.FromSeconds(5); private static readonly Dictionary RateLimiters = new();