Implement dynamic request throttle configuration; update HttpClient initialization

This commit is contained in:
Andre Beging
2026-03-04 11:43:08 +01:00
parent a145c059ac
commit 71e2c8df92
2 changed files with 17 additions and 2 deletions

View File

@@ -10,7 +10,8 @@
], ],
"env": { "env": {
"USERNAME": "mail@address.com", "USERNAME": "mail@address.com",
"PASSWORD": "sup3rsecur3" "PASSWORD": "sup3rsecur3",
"REQUEST_THROTTLE_MS": "500"
} }
} }
} }

View File

@@ -4,9 +4,23 @@ namespace FsMcp;
internal sealed class FoodsharingApiClient internal sealed class FoodsharingApiClient
{ {
private const string RequestThrottleMsEnvVar = "REQUEST_THROTTLE_MS";
private readonly SemaphoreSlim _loginLock = new(1, 1); private readonly SemaphoreSlim _loginLock = new(1, 1);
public HttpClient HttpClient { get; } = new(new RequestThrottleHandler(TimeSpan.FromMilliseconds(200))); public HttpClient HttpClient { get; } = new(new RequestThrottleHandler(TimeSpan.FromMilliseconds(GetRequestThrottleMs())));
private static int GetRequestThrottleMs()
{
string? configuredValue = Environment.GetEnvironmentVariable(RequestThrottleMsEnvVar);
if (int.TryParse(configuredValue, out int throttleMs) && throttleMs >= 0)
{
return throttleMs;
}
return 500;
}
public async Task EnsureLoginAsync() public async Task EnsureLoginAsync()
{ {