singletons HTTP client

This commit is contained in:
Ben 2025-09-04 14:08:03 +02:00
parent 72e5a1995d
commit cfe2c47d95
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
2 changed files with 25 additions and 8 deletions

View File

@ -79,10 +79,25 @@ namespace WebUtils
private HttpClient GetClient() private HttpClient GetClient()
{ {
var client = new HttpClient(); return HttpClientSingleton.Get(timeSet.HttpCallTimeout(), onClientCreated);
client.Timeout = timeSet.HttpCallTimeout(); }
onClientCreated(client); }
return client;
public static class HttpClientSingleton
{
private static readonly Dictionary<TimeSpan, HttpClient> instances = new();
public static HttpClient Get(TimeSpan timeout, Action<HttpClient> onClientCreated)
{
if (!instances.ContainsKey(timeout))
{
var client = new HttpClient();
client.Timeout = timeout;
onClientCreated(client);
instances.Add(timeout, client);
}
return instances[timeout];
} }
} }
} }

View File

@ -4,12 +4,15 @@ using Logging;
using NUnit.Framework; using NUnit.Framework;
using Utils; using Utils;
namespace CodexReleaseTests.DataTests namespace CodexReleaseTests.DataTests.DHT
{ {
[Ignore("work in progress")] [Ignore("work in progress")]
[TestFixture(10, 10000)] [TestFixture(10, 1000)]
[TestFixture(50, 1000)] [TestFixture(50, 1000)]
[TestFixture(100, 1000)] [TestFixture(10, 10000)]
[TestFixture(20, 10000)]
[TestFixture(30, 10000)]
[TestFixture(50, 10000)]
public class DhtTest : AutoBootstrapDistTest public class DhtTest : AutoBootstrapDistTest
{ {
public DhtTest(int nodes, int files) public DhtTest(int nodes, int files)
@ -50,7 +53,6 @@ namespace CodexReleaseTests.DataTests
node.DownloadContent(cids.PickOneRandom()); node.DownloadContent(cids.PickOneRandom());
}); });
Log(Time.FormatDuration(timing));
Assert.That(timing, Is.LessThan(maxDownloadDuration)); Assert.That(timing, Is.LessThan(maxDownloadDuration));
} }
} }