diff --git a/ClickHouse.Driver/Http/DefaultPoolHttpClientFactory.cs b/ClickHouse.Driver/Http/DefaultPoolHttpClientFactory.cs index e92e0da6..462370df 100644 --- a/ClickHouse.Driver/Http/DefaultPoolHttpClientFactory.cs +++ b/ClickHouse.Driver/Http/DefaultPoolHttpClientFactory.cs @@ -5,13 +5,13 @@ namespace ClickHouse.Driver.Http; internal class DefaultPoolHttpClientFactory : IHttpClientFactory, IDisposable { - private readonly DefaultHttpClientHandler handler; + private readonly HttpMessageHandler handler; public TimeSpan Timeout { get; init; } public DefaultPoolHttpClientFactory(bool skipServerCertificateValidation) { - handler = new(skipServerCertificateValidation); + handler = HttpHandlerProvider.CreateHandler(skipServerCertificateValidation); } public HttpClient CreateClient(string name) => new(handler, false) { Timeout = Timeout }; diff --git a/ClickHouse.Driver/Http/HttpHandlerProvider.cs b/ClickHouse.Driver/Http/HttpHandlerProvider.cs new file mode 100644 index 00000000..fc4658e2 --- /dev/null +++ b/ClickHouse.Driver/Http/HttpHandlerProvider.cs @@ -0,0 +1,41 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Net.Security; + +namespace ClickHouse.Driver.Http; + +internal static class HttpHandlerProvider +{ + public static HttpMessageHandler CreateHandler(bool skipServerCertificateValidation, int? maxConnectionsPerServer = null) + { +#if NETCOREAPP2_1_OR_GREATER + var socketsHandler = new SocketsHttpHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, + PooledConnectionIdleTimeout = TimeSpan.FromSeconds(5), + }; + + if (maxConnectionsPerServer.HasValue) + { + socketsHandler.MaxConnectionsPerServer = maxConnectionsPerServer.Value; + } + + if (skipServerCertificateValidation) + { + socketsHandler.SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + }; + } + return socketsHandler; +#else + var defaultHandler = new DefaultHttpClientHandler(skipServerCertificateValidation); + if (maxConnectionsPerServer.HasValue) + { + defaultHandler.MaxConnectionsPerServer = maxConnectionsPerServer.Value; + } + return defaultHandler; +#endif + } +} diff --git a/ClickHouse.Driver/Http/SingleConnectionHttpClientFactory.cs b/ClickHouse.Driver/Http/SingleConnectionHttpClientFactory.cs index be23125f..95688e5b 100644 --- a/ClickHouse.Driver/Http/SingleConnectionHttpClientFactory.cs +++ b/ClickHouse.Driver/Http/SingleConnectionHttpClientFactory.cs @@ -5,13 +5,13 @@ namespace ClickHouse.Driver.Http; internal class SingleConnectionHttpClientFactory : IHttpClientFactory, IDisposable { - private readonly DefaultHttpClientHandler handler; + private readonly HttpMessageHandler handler; public TimeSpan Timeout { get; init; } public SingleConnectionHttpClientFactory(bool skipServerCertificateValidation) { - handler = new(skipServerCertificateValidation) { MaxConnectionsPerServer = 1 }; + handler = HttpHandlerProvider.CreateHandler(skipServerCertificateValidation, maxConnectionsPerServer: 1); } public HttpClient CreateClient(string name) => new(handler, false) { Timeout = Timeout };