-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly set connection lease timeout regardless of .NET target frame…
- Loading branch information
1 parent
be0a5a5
commit 6027e88
Showing
2 changed files
with
57 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
||
namespace Microsoft.Azure.Devices.Client | ||
{ | ||
// This type manages changing HttpClient defaults to more appropriate values | ||
// There are two limits we target: | ||
// - Per Server Connection Limit | ||
// - Keep Alive Connection Timeout | ||
// On .NET Core 2.1+ the HttpClient defaults to using the HttpSocketHandler so we adjust both limits on the client handler | ||
// | ||
// On .NET Standard & NET 4.51+ the HttpClient defaults to using the HttpClientHandler | ||
// and there is no easy way to set Keep Alive Connection Timeout but it's mitigated by setting the service point's connection lease timeout | ||
internal static class ServicePointHelpers | ||
{ | ||
// These default values are consistent with Azure.Core default values: | ||
// https://github.com/Azure/azure-sdk-for-net/blob/7e3cf643977591e9041f4c628fd4d28237398e0b/sdk/core/Azure.Core/src/Pipeline/ServicePointHelpers.cs#L28 | ||
internal const int DefaultMaxConnectionsPerServer = 50; | ||
|
||
internal const int DefaultConnectionLeaseTimeout = 300 * 1000; // 5 minutes | ||
|
||
// messageHandler passed in is an HttpClientHandler for .NET Framework and .NET standard, and a SocketsHttpHandler for .NET core | ||
public static void SetLimits(HttpMessageHandler messageHandler, Uri baseUri, int connectionLeaseTimeoutMilliseconds = DefaultConnectionLeaseTimeout) | ||
{ | ||
if (messageHandler == null) | ||
{ | ||
// no limits can be set if no handler is provided | ||
return; | ||
} | ||
|
||
switch (messageHandler) | ||
{ | ||
case HttpClientHandler httpClientHandler: | ||
#if !NET451 | ||
httpClientHandler.MaxConnectionsPerServer = DefaultMaxConnectionsPerServer; | ||
#endif | ||
ServicePoint servicePoint = ServicePointManager.FindServicePoint(baseUri); | ||
servicePoint.ConnectionLeaseTimeout = connectionLeaseTimeoutMilliseconds; | ||
break; | ||
#if NETCOREAPP2_1_OR_GREATER || NET5_0_OR_GREATER | ||
// SocketsHttpHandler is only available in netcore2.1 and onwards | ||
case SocketsHttpHandler socketsHttpHandler: | ||
socketsHttpHandler.MaxConnectionsPerServer = DefaultMaxConnectionsPerServer; | ||
socketsHttpHandler.PooledConnectionLifetime = TimeSpan.FromMilliseconds(connectionLeaseTimeoutMilliseconds); | ||
break; | ||
#endif | ||
} | ||
} | ||
} | ||
} |