Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,43 @@ namespace Elasticsearch.Net
/// </summary>
public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfiguration>
{
/// <summary>
/// Detects whether we are running on .NET Core with CurlHandler.
/// If this is true, we will set a very restrictive <see cref="DefaultConnectionLimit"/>
/// As the old curl based handler is known to bleed TCP connections:
/// <para>https://github.com/dotnet/runtime/issues/22366</para>
/// </summary>
private static bool UsingCurlHandler
{
get
{
#if !DOTNETCORE
return false;
#else
var curlHandlerExists = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.CurlHandler") != null;
if (!curlHandlerExists) return false;

var socketsHandlerExists = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.SocketsHttpHandler") != null;
// running on a .NET core version with CurlHandler, before the existence of SocketsHttpHandler.
// Must be using CurlHandler.
if (!socketsHandlerExists) return true;

if (AppContext.TryGetSwitch("System.Net.Http.UseSocketsHttpHandler", out var isEnabled))
return !isEnabled;

var environmentVariable =
Environment.GetEnvironmentVariable("DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER");

// SocketsHandler exists and no environment variable exists to disable it.
// Must be using SocketsHandler and not CurlHandler
if (environmentVariable == null) return false;

return environmentVariable.Equals("false", StringComparison.OrdinalIgnoreCase) ||
environmentVariable.Equals("0");
#endif
}
}

/// <summary>
/// The default ping timeout. Defaults to 2 seconds
/// </summary>
Expand All @@ -40,12 +77,12 @@ public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfigu
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromMinutes(1);

/// <summary>
/// The default connection limit for both Elasticsearch.Net and Nest. Defaults to <c>80</c> except for
/// HttpClientHandler implementations based on curl, which defaults to
/// <see cref="Environment.ProcessorCount" />
/// The default connection limit for both Elasticsearch.Net and Nest. Defaults to <c>80</c>
#if DOTNETCORE
/// <para>Except for <see cref="HttpClientHandler"/> implementations based on curl, which defaults to <see cref="Environment.ProcessorCount"/></para>
#endif
/// </summary>
public static readonly int DefaultConnectionLimit = IsCurlHandler ? Environment.ProcessorCount : 80;

public static readonly int DefaultConnectionLimit = UsingCurlHandler ? Environment.ProcessorCount : 80;

/// <summary>
/// The default user agent for Elasticsearch.Net
Expand Down