Skip to content

Commit 1a54cdd

Browse files
russcamMpdreamz
authored andcommitted
Fix RequestData hashcode and equality for .NET core HttpConnection (#2420)
RequestData's GetHashCode implementation uses instances of types that will return different values per instance (Headers, BasicAuthenticationCredentials). Remove overridden GetHashCode and Equals methods from RequestData and move hashcode generation into the HttpConnection-CoreFx implementation as it is only used there. Fixes #2417
1 parent 8bbd839 commit 1a54cdd

File tree

3 files changed

+32
-69
lines changed

3 files changed

+32
-69
lines changed

src/Elasticsearch.Net/Configuration/Security/BasicAuthenticationCredentials.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace Elasticsearch.Net
1+
namespace Elasticsearch.Net
42
{
53
public class BasicAuthenticationCredentials
64
{

src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,32 @@ internal class WebProxy : IWebProxy
3131
public class HttpConnection : IConnection
3232
{
3333
private readonly object _lock = new object();
34+
3435
private readonly ConcurrentDictionary<int, HttpClient> _clients = new ConcurrentDictionary<int, HttpClient>();
3536

3637
private HttpClient GetClient(RequestData requestData)
3738
{
38-
var hashCode = requestData.GetHashCode();
39+
var key = GetClientKey(requestData);
3940
HttpClient client;
40-
if (this._clients.TryGetValue(hashCode, out client)) return client;
41-
lock (_lock)
41+
if (!this._clients.TryGetValue(key, out client))
4242
{
43-
if (this._clients.TryGetValue(hashCode, out client)) return client;
44-
45-
var handler = CreateHttpClientHandler(requestData);
46-
47-
client = new HttpClient(handler, false)
43+
lock (_lock)
4844
{
49-
Timeout = requestData.RequestTimeout
50-
};
51-
52-
client.DefaultRequestHeaders.ExpectContinue = false;
53-
54-
this._clients.TryAdd(hashCode, client);
55-
return client;
45+
client = this._clients.GetOrAdd(key, h =>
46+
{
47+
var handler = CreateHttpClientHandler(requestData);
48+
var httpClient = new HttpClient(handler, false)
49+
{
50+
Timeout = requestData.RequestTimeout
51+
};
52+
53+
httpClient.DefaultRequestHeaders.ExpectContinue = false;
54+
return httpClient;
55+
});
56+
}
5657
}
5758

59+
return client;
5860
}
5961

6062
public virtual ElasticsearchResponse<TReturn> Request<TReturn>(RequestData requestData) where TReturn : class
@@ -189,7 +191,6 @@ protected virtual void SetBasicAuthenticationIfNeeded(HttpRequestMessage request
189191
}
190192
}
191193

192-
193194
private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMethod)
194195
{
195196
switch (httpMethod)
@@ -204,6 +205,20 @@ private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMetho
204205
}
205206
}
206207

208+
private static int GetClientKey(RequestData requestData)
209+
{
210+
unchecked
211+
{
212+
var hashCode = requestData.RequestTimeout.GetHashCode();
213+
hashCode = (hashCode * 397) ^ requestData.HttpCompression.GetHashCode();
214+
hashCode = (hashCode * 397) ^ (requestData.ProxyAddress?.GetHashCode() ?? 0);
215+
hashCode = (hashCode * 397) ^ (requestData.ProxyUsername?.GetHashCode() ?? 0);
216+
hashCode = (hashCode * 397) ^ (requestData.ProxyPassword?.GetHashCode() ?? 0);
217+
hashCode = (hashCode * 397) ^ requestData.DisableAutomaticProxyDetection.GetHashCode();
218+
return hashCode;
219+
}
220+
}
221+
207222
void IDisposable.Dispose() => this.DisposeManagedResources();
208223

209224
protected virtual void DisposeManagedResources()

src/Elasticsearch.Net/Transport/Pipeline/RequestData.cs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -112,55 +112,5 @@ private string CreatePathWithQueryStrings(string path, IConnectionConfigurationV
112112
path += "&" + queryString.Substring(1, queryString.Length - 1);
113113
return path;
114114
}
115-
116-
117-
protected bool Equals(RequestData other) =>
118-
RequestTimeout.Equals(other.RequestTimeout)
119-
&& PingTimeout.Equals(other.PingTimeout)
120-
&& KeepAliveTime == other.KeepAliveTime
121-
&& KeepAliveInterval == other.KeepAliveInterval
122-
&& Pipelined == other.Pipelined
123-
&& HttpCompression == other.HttpCompression
124-
&& Equals(Headers, other.Headers)
125-
&& string.Equals(RunAs, other.RunAs)
126-
&& string.Equals(ProxyAddress, other.ProxyAddress)
127-
&& string.Equals(ProxyUsername, other.ProxyUsername)
128-
&& string.Equals(ProxyPassword, other.ProxyPassword)
129-
&& DisableAutomaticProxyDetection == other.DisableAutomaticProxyDetection
130-
&& Equals(BasicAuthorizationCredentials, other.BasicAuthorizationCredentials)
131-
&& Equals(ConnectionSettings, other.ConnectionSettings)
132-
&& Equals(MemoryStreamFactory, other.MemoryStreamFactory);
133-
134-
public override bool Equals(object obj)
135-
{
136-
if (ReferenceEquals(null, obj)) return false;
137-
if (ReferenceEquals(this, obj)) return true;
138-
if (obj.GetType() != this.GetType()) return false;
139-
return Equals((RequestData) obj);
140-
}
141-
142-
public override int GetHashCode()
143-
{
144-
unchecked
145-
{
146-
var hashCode = RequestTimeout.GetHashCode();
147-
hashCode = (hashCode*397) ^ PingTimeout.GetHashCode();
148-
hashCode = (hashCode*397) ^ KeepAliveTime;
149-
hashCode = (hashCode*397) ^ KeepAliveInterval;
150-
hashCode = (hashCode*397) ^ (RunAs?.GetHashCode() ?? 0);
151-
hashCode = (hashCode*397) ^ Pipelined.GetHashCode();
152-
hashCode = (hashCode*397) ^ HttpCompression.GetHashCode();
153-
hashCode = (hashCode*397) ^ (Headers?.GetHashCode() ?? 0);
154-
hashCode = (hashCode*397) ^ (ProxyAddress?.GetHashCode() ?? 0);
155-
hashCode = (hashCode*397) ^ (ProxyUsername?.GetHashCode() ?? 0);
156-
hashCode = (hashCode*397) ^ (ProxyPassword?.GetHashCode() ?? 0);
157-
hashCode = (hashCode*397) ^ DisableAutomaticProxyDetection.GetHashCode();
158-
hashCode = (hashCode*397) ^ (BasicAuthorizationCredentials?.GetHashCode() ?? 0);
159-
hashCode = (hashCode*397) ^ (ConnectionSettings?.GetHashCode() ?? 0);
160-
hashCode = (hashCode*397) ^ (MemoryStreamFactory?.GetHashCode() ?? 0);
161-
return hashCode;
162-
}
163-
}
164-
165115
}
166116
}

0 commit comments

Comments
 (0)