Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rate limit test more lenient #311

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading