Skip to content

Commit fc88ff7

Browse files
authored
fix: Fix NRHttpClientFactory so that it creates only one client. (#1873)
1 parent a78abc9 commit fc88ff7

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using NewRelic.Agent.Configuration;
1111
using NewRelic.Agent.Core.DataTransport.Client.Interfaces;
12+
using NewRelic.Core.Logging;
1213

1314
namespace NewRelic.Agent.Core.DataTransport.Client
1415
{
@@ -25,7 +26,9 @@ public NRHttpClient(IWebProxy proxy, IConfiguration configuration) : base(proxy)
2526
_configuration = configuration;
2627

2728
// set the default timeout to "infinite", but specify the configured collector timeout as the actual timeout for SendAsync() calls
28-
var httpClient = new HttpClient(new HttpClientHandler { Proxy = proxy }, true) {Timeout = System.Threading.Timeout.InfiniteTimeSpan};
29+
var httpHandler = new HttpClientHandler { Proxy = proxy };
30+
Log.InfoFormat("Current HttpClientHandler TLS Configuration (HttpClientHandler.SslProtocols): {0}", httpHandler.SslProtocols.ToString());
31+
var httpClient = new HttpClient(httpHandler, true) {Timeout = System.Threading.Timeout.InfiniteTimeSpan};
2932
_httpClientWrapper = new HttpClientWrapper(httpClient, (int)configuration.CollectorTimeout);
3033
}
3134

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ public class NRHttpClientFactory : IHttpClientFactory
1717
{
1818
private IHttpClient _httpClient;
1919

20+
private bool? _hasProxy;
21+
2022
public IHttpClient CreateClient(IWebProxy proxy, IConfiguration configuration)
2123
{
22-
if (proxy != null)
23-
{
24-
Interlocked.CompareExchange(ref _httpClient, new NRHttpClient(proxy, configuration), null);
25-
}
26-
else
24+
var proxyRequired = (proxy != null);
25+
if (_httpClient != null && (_hasProxy == proxyRequired))
2726
{
28-
Interlocked.CompareExchange(ref _httpClient, new NRHttpClient(null,configuration), null);
27+
return _httpClient;
2928
}
3029

31-
return _httpClient;
30+
_hasProxy = proxyRequired;
31+
return _httpClient = new NRHttpClient(proxy, configuration);
3232
}
3333
}
3434
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2020 New Relic, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#if !NETFRAMEWORK
5+
using System;
6+
using System.Net;
7+
using System.Net.Http;
8+
using System.Threading.Tasks;
9+
using NewRelic.Agent.Configuration;
10+
using NewRelic.Agent.Core.DataTransport.Client.Interfaces;
11+
using NUnit.Framework;
12+
using Telerik.JustMock;
13+
using Telerik.JustMock.Helpers;
14+
15+
namespace NewRelic.Agent.Core.DataTransport.Client
16+
{
17+
internal class NRHttpClientFactoryTests
18+
{
19+
private IConfiguration _mockConfiguration;
20+
private IWebProxy _mockProxy;
21+
private IHttpClientFactory _httpClientFactory;
22+
23+
[SetUp]
24+
public void SetUp()
25+
{
26+
_mockConfiguration = Mock.Create<IConfiguration>();
27+
Mock.Arrange(() => _mockConfiguration.AgentLicenseKey).Returns("12345");
28+
Mock.Arrange(() => _mockConfiguration.AgentRunId).Returns("123");
29+
Mock.Arrange(() => _mockConfiguration.CollectorMaxPayloadSizeInBytes).Returns(int.MaxValue);
30+
31+
_mockProxy = Mock.Create<IWebProxy>();
32+
33+
_httpClientFactory = new NRHttpClientFactory();
34+
}
35+
36+
[Test]
37+
public void CreateClient_NotNull()
38+
{
39+
var client = _httpClientFactory.CreateClient(null, _mockConfiguration);
40+
41+
Assert.That(client, Is.Not.Null);
42+
}
43+
44+
[Test]
45+
public void CreateClient_NoProxy_ReturnsSameClient()
46+
{
47+
var clientA = _httpClientFactory.CreateClient(null, _mockConfiguration);
48+
var clientB = _httpClientFactory.CreateClient(null, _mockConfiguration);
49+
50+
Assert.That(clientA == clientB);
51+
}
52+
53+
[Test]
54+
public void CreateClient_Proxy_ReturnsSameClient()
55+
{
56+
var clientA = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration);
57+
var clientB = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration);
58+
59+
Assert.That(clientA == clientB);
60+
}
61+
62+
[Test]
63+
public void CreateClient_NoProxyToProxy_ReturnsNewClient()
64+
{
65+
var clientA = _httpClientFactory.CreateClient(null, _mockConfiguration);
66+
var clientB = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration);
67+
68+
Assert.That(clientA != clientB);
69+
}
70+
71+
[Test]
72+
public void CreateClient_ProxyToNoProxy_ReturnsNewClient()
73+
{
74+
var clientA = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration);
75+
var clientB = _httpClientFactory.CreateClient(null, _mockConfiguration);
76+
77+
Assert.That(clientA != clientB);
78+
}
79+
}
80+
}
81+
#endif

0 commit comments

Comments
 (0)