ClearnetHttpClient: HttpClient need to be passed as a dependency now. WasabiClientFactory creates an HttpClient singleton and disposes that when app terminates.
This commit breaks build as tests need to be updated too.
MODIFY
WalletWasabi/Tor/Http/ClearnetHttpClient.cs
WalletWasabi/Tor/Http/ClearnetHttpClient.cs
using System;
-using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
@@ -13,27 +12,16 @@ namespace WalletWasabi.Tor.Http
/// <remarks>Inner <see cref="HttpClient"/> instance is thread-safe.</remarks>
public class ClearnetHttpClient : IRelativeHttpClient
{
- static ClearnetHttpClient()
- {
- var socketHandler = new SocketsHttpHandler()
- {
- // Only GZip is currently used by Wasabi Backend.
- AutomaticDecompression = DecompressionMethods.GZip,
- PooledConnectionLifetime = TimeSpan.FromMinutes(5)
- };
-
- HttpClient = new HttpClient(socketHandler);
- }
-
- public ClearnetHttpClient(Func<Uri> destinationUriAction)
+ public ClearnetHttpClient(HttpClient httpClient, Func<Uri> destinationUriAction)
{
DestinationUriAction = destinationUriAction;
+ HttpClient = httpClient;
}
public Func<Uri> DestinationUriAction { get; }
/// <summary>Predefined HTTP client that handles HTTP requests when Tor is disabled.</summary>
- private static HttpClient HttpClient { get; }
+ private HttpClient HttpClient { get; }
/// <summary>The value is not used at the moment.</summary>
/// <remarks>

MODIFY
WalletWasabi/Tor/Http/TorHttpClient.cs
WalletWasabi/Tor/Http/TorHttpClient.cs
@@ -15,6 +15,7 @@ using WalletWasabi.Tor.Http.Models;
using WalletWasabi.Tor.Socks5;
using WalletWasabi.Tor.Socks5.Exceptions;
using WalletWasabi.Tor.Socks5.Models.Fields.OctetFields;
+using WalletWasabi.WebClients.Wasabi;
namespace WalletWasabi.Tor.Http
{
@@ -70,7 +71,7 @@ namespace WalletWasabi.Tor.Http
private Task<HttpResponseMessage> ClearnetRequestAsync(HttpRequestMessage request, bool isolateStream, CancellationToken cancellationToken = default)
{
- return new ClearnetHttpClient(DestinationUriAction).SendAsync(request, isolateStream, cancellationToken);
+ return new ClearnetHttpClient(WasabiClientFactory.HttpClient, DestinationUriAction).SendAsync(request, isolateStream, cancellationToken);
}
/// <remarks>

MODIFY
WalletWasabi/WebClients/Wasabi/WasabiClientFactory.cs
WalletWasabi/WebClients/Wasabi/WasabiClientFactory.cs
using System;
using System.Net;
+using System.Net.Http;
using WalletWasabi.Tor.Http;
namespace WalletWasabi.WebClients.Wasabi
@@ -14,6 +15,19 @@ namespace WalletWasabi.WebClients.Wasabi
/// </summary>
private bool _disposed = false;
+ /// <summary>Temporary solution until #4738 PR is merged. Static constructor will be removed then.</summary>
+ static WasabiClientFactory()
+ {
+ SocketHandler = new SocketsHttpHandler()
+ {
+ // Only GZip is currently used by Wasabi Backend.
+ AutomaticDecompression = DecompressionMethods.GZip,
+ PooledConnectionLifetime = TimeSpan.FromMinutes(5)
+ };
+
+ HttpClient = new HttpClient(SocketHandler);
+ }
+
/// <summary>
/// Creates a new instance of the object.
/// </summary>
@@ -29,7 +43,7 @@ namespace WalletWasabi.WebClients.Wasabi
}
else
{
- BackendHttpClient = new ClearnetHttpClient(BackendUriGetter);
+ BackendHttpClient = new ClearnetHttpClient(HttpClient, BackendUriGetter);
}
SharedWasabiClient = new WasabiClient(BackendHttpClient);
@@ -45,6 +59,13 @@ namespace WalletWasabi.WebClients.Wasabi
/// <summary>Whether Tor is enabled or disabled.</summary>
public bool IsTorEnabled => TorEndpoint is { };
+ /// <remarks>The goal is to make this field non-static when #4738 PR is merged.</remarks>
+ private static SocketsHttpHandler SocketHandler;
+
+ /// <summary>.NET HTTP client to be used by <see cref="ClearnetHttpClient"/> instances.</summary>
+ /// <remarks>The goal is to make this field non-static when #4738 PR is merged.</remarks>
+ public static HttpClient HttpClient { get; }
+
/// <summary>Backend HTTP client, shared instance.</summary>
private IRelativeHttpClient BackendHttpClient { get; }
@@ -70,7 +91,7 @@ namespace WalletWasabi.WebClients.Wasabi
}
else
{
- return new ClearnetHttpClient(baseUriFn);
+ return new ClearnetHttpClient(HttpClient, baseUriFn);
}
}
@@ -97,6 +118,9 @@ namespace WalletWasabi.WebClients.Wasabi
{
httpClient.Dispose();
}
+
+ HttpClient.Dispose();
+ SocketHandler.Dispose();
}
_disposed = true;

ClearnetHttpClient: HttpClient need to be passed as a dependency now. WasabiClientFactory creates an HttpClient singleton and disposes that when app terminates.

This commit breaks build as tests need to be updated too.