Skip to content

Commit d489a05

Browse files
committed
fix: Specify a non-infinite timeout when creating HttpClient and SocketsHttpHandler instances.
1 parent 358c4f0 commit d489a05

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/Agent/NewRelic/Agent/Core/DataTransport/Client/NRHttpClient.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ namespace NewRelic.Agent.Core.DataTransport.Client
1818
/// </summary>
1919
public class NRHttpClient : HttpClientBase
2020
{
21-
private readonly IConfiguration _configuration;
2221
private IHttpClientWrapper _httpClientWrapper;
22+
private readonly TimeSpan _timeout;
23+
private readonly HttpMethod _httpMethod;
2324

2425
public NRHttpClient(IWebProxy proxy, IConfiguration configuration) : base(proxy)
2526
{
26-
_configuration = configuration;
27+
_timeout = TimeSpan.FromMilliseconds((int)configuration.CollectorTimeout);
28+
_httpMethod = configuration.PutForDataSend ? HttpMethod.Put : HttpMethod.Post;
2729

2830
// set the default timeout to "infinite", but specify the configured collector timeout as the actual timeout for SendAsync() calls
2931
var httpHandler = GetHttpHandler(proxy);
3032

31-
var httpClient = new HttpClient(httpHandler, true) { Timeout = System.Threading.Timeout.InfiniteTimeSpan };
32-
_httpClientWrapper = new HttpClientWrapper(httpClient, (int)configuration.CollectorTimeout);
33+
var httpClient = new HttpClient(httpHandler, true) { Timeout = _timeout };
34+
_httpClientWrapper = new HttpClientWrapper(httpClient, (int)_timeout.TotalMilliseconds);
3335
}
3436

3537
private dynamic GetHttpHandler(IWebProxy proxy)
@@ -42,7 +44,7 @@ private dynamic GetHttpHandler(IWebProxy proxy)
4244
var pooledConnectionLifetime = TimeSpan.FromMinutes(5); // an in-use connection will be closed and recycled after 5 minutes
4345
var pooledConnectionIdleTimeout = TimeSpan.FromMinutes(1); // a connection that is idle for 1 minute will be closed and recycled
4446

45-
Log.Info($"Creating a SocketsHttpHandler with PooledConnectionLifetime set to {pooledConnectionLifetime} and PooledConnectionIdleTimeout set to {pooledConnectionIdleTimeout}");
47+
Log.Info($"Creating a SocketsHttpHandler with PooledConnectionLifetime {pooledConnectionLifetime}, PooledConnectionIdleTimeout {pooledConnectionIdleTimeout} and ConnectTimeout {_timeout}");
4648

4749
// use reflection to create a SocketsHttpHandler instance and set the PooledConnectionLifetime to 1 minute
4850
var assembly = Assembly.Load("System.Net.Http");
@@ -51,6 +53,7 @@ private dynamic GetHttpHandler(IWebProxy proxy)
5153

5254
handler.PooledConnectionLifetime = pooledConnectionLifetime;
5355
handler.PooledConnectionIdleTimeout = pooledConnectionIdleTimeout;
56+
handler.ConnectTimeout = _timeout;
5457

5558
handler.Proxy = proxy;
5659

@@ -77,7 +80,7 @@ public override IHttpResponse Send(IHttpRequest request)
7780
{
7881
using var req = new HttpRequestMessage();
7982
req.RequestUri = request.Uri;
80-
req.Method = _configuration.PutForDataSend ? HttpMethod.Put : HttpMethod.Post;
83+
req.Method = _httpMethod;
8184
req.Headers.Add("User-Agent", $"NewRelic-DotNetAgent/{AgentInstallConfiguration.AgentVersion}");
8285
req.Headers.Add("Connection", "keep-alive");
8386
req.Headers.Add("Keep-Alive", "true");

0 commit comments

Comments
 (0)