Skip to content

Commit

Permalink
Make rate limit test more lenient (#311)
Browse files Browse the repository at this point in the history
* Make rate limit test more lenient

* Fix spelling of Throttle and increase rate limiter test maximum interval to 65s
  • Loading branch information
adamelfstrom authored Dec 3, 2024
1 parent 2898102 commit efa3f50
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion FortnoxSDK.Tests/RateLimiterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion FortnoxSDK/Connectors/Base/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<byte[]> 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);

Expand Down
6 changes: 3 additions & 3 deletions FortnoxSDK/Connectors/Base/RateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Fortnox.SDK.Connectors.Base;
/// Rate limit - 4 requests per 1 seconds per token
/// </summary>
internal class RateLimiter
{
{
private const int MaxCount = 4;
private static readonly TimeSpan Period = TimeSpan.FromSeconds(1);

private static readonly Dictionary<string, TimeLimiter> RateLimiters = new();

public async Task Trottle(string token)
public async Task Throttle(string token)
{
if (token == null)
return;
Expand Down

0 comments on commit efa3f50

Please sign in to comment.