Skip to content

Commit ee2ae5e

Browse files
authored
[Backport 6x] Fix check for agressive connectionlimit (#4669)
In dotnet/runtime#22366 we found that if `HttpClient` is using the curl handler it will lead to TCP connections bleeding. Our DefaultConnectionLimit is very restrictive if this is true. Our check however is too lenient and will default to true always on .NET core since netcoreapp still ships with CurlHandler as recent as `3.1.x`
1 parent 236c83b commit ee2ae5e

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ namespace Elasticsearch.Net
2222
/// </summary>
2323
public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfiguration>
2424
{
25+
/// <summary>
26+
/// Detects whether we are running on .NET Core with CurlHandler.
27+
/// If this is true, we will set a very restrictive <see cref="DefaultConnectionLimit"/>
28+
/// As the old curl based handler is known to bleed TCP connections:
29+
/// <para>https://github.com/dotnet/runtime/issues/22366</para>
30+
/// </summary>
31+
private static bool UsingCurlHandler
32+
{
33+
get
34+
{
35+
#if !DOTNETCORE
36+
return false;
37+
#else
38+
var curlHandlerExists = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.CurlHandler") != null;
39+
if (!curlHandlerExists) return false;
40+
41+
var socketsHandlerExists = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.SocketsHttpHandler") != null;
42+
// running on a .NET core version with CurlHandler, before the existence of SocketsHttpHandler.
43+
// Must be using CurlHandler.
44+
if (!socketsHandlerExists) return true;
45+
46+
if (AppContext.TryGetSwitch("System.Net.Http.UseSocketsHttpHandler", out var isEnabled))
47+
return !isEnabled;
48+
49+
var environmentVariable =
50+
Environment.GetEnvironmentVariable("DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER");
51+
52+
// SocketsHandler exists and no environment variable exists to disable it.
53+
// Must be using SocketsHandler and not CurlHandler
54+
if (environmentVariable == null) return false;
55+
56+
return environmentVariable.Equals("false", StringComparison.OrdinalIgnoreCase) ||
57+
environmentVariable.Equals("0");
58+
#endif
59+
}
60+
}
61+
2562
/// <summary>
2663
/// The default ping timeout. Defaults to 2 seconds
2764
/// </summary>
@@ -40,12 +77,12 @@ public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfigu
4077
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromMinutes(1);
4178

4279
/// <summary>
43-
/// The default connection limit for both Elasticsearch.Net and Nest. Defaults to <c>80</c> except for
44-
/// HttpClientHandler implementations based on curl, which defaults to
45-
/// <see cref="Environment.ProcessorCount" />
80+
/// The default connection limit for both Elasticsearch.Net and Nest. Defaults to <c>80</c>
81+
#if DOTNETCORE
82+
/// <para>Except for <see cref="HttpClientHandler"/> implementations based on curl, which defaults to <see cref="Environment.ProcessorCount"/></para>
83+
#endif
4684
/// </summary>
47-
public static readonly int DefaultConnectionLimit = IsCurlHandler ? Environment.ProcessorCount : 80;
48-
85+
public static readonly int DefaultConnectionLimit = UsingCurlHandler ? Environment.ProcessorCount : 80;
4986

5087
/// <summary>
5188
/// The default user agent for Elasticsearch.Net

0 commit comments

Comments
 (0)