Skip to content

Commit d21d604

Browse files
github-actions[bot]Mpdreamzrusscam
authored
[Backport 7.x] Fix check for agressive connectionlimit (#4668)
* Fix check for agressive connectionlimit 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` * remove unused reference * Detect if CurlHandler exists This commit updates the implementation of determining if a conservative default connection limit should be set by also checking if CurlHandler exists when UseSocketsHttpHandler exists and is disabled. Co-authored-by: Russ Cam <[email protected]> Co-authored-by: Martijn Laarman <[email protected]> Co-authored-by: Russ Cam <[email protected]>
1 parent 6a8ea82 commit d21d604

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs

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

3263
/// <summary>
3364
/// The default ping timeout. Defaults to 2 seconds
@@ -52,7 +83,7 @@ public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfigu
5283
/// <para>Except for <see cref="HttpClientHandler"/> implementations based on curl, which defaults to <see cref="Environment.ProcessorCount"/></para>
5384
#endif
5485
/// </summary>
55-
public static readonly int DefaultConnectionLimit = IsCurlHandler ? Environment.ProcessorCount : 80;
86+
public static readonly int DefaultConnectionLimit = UsingCurlHandler ? Environment.ProcessorCount : 80;
5687

5788
/// <summary>
5889
/// The default user agent for Elasticsearch.Net

0 commit comments

Comments
 (0)