Skip to content

Commit ddb78a9

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 c01f045 commit ddb78a9

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
@@ -30,6 +30,7 @@ internal class WebProxy : IWebProxy
3030
public class HttpConnection : IConnection
3131
{
3232
private readonly object _lock = new object();
33+
3334
private readonly ConcurrentDictionary<int, HttpClient> _clients = new ConcurrentDictionary<int, HttpClient>();
3435

3536
private string DefaultContentType => "application/json";
@@ -38,26 +39,27 @@ public HttpConnection() { }
3839

3940
private HttpClient GetClient(RequestData requestData)
4041
{
41-
var hashCode = requestData.GetHashCode();
42+
var key = GetClientKey(requestData);
4243
HttpClient client;
43-
if (this._clients.TryGetValue(hashCode, out client)) return client;
44-
lock (_lock)
44+
if (!this._clients.TryGetValue(key, out client))
4545
{
46-
if (this._clients.TryGetValue(hashCode, out client)) return client;
47-
48-
var handler = CreateHttpClientHandler(requestData);
49-
50-
client = new HttpClient(handler, false)
46+
lock (_lock)
5147
{
52-
Timeout = requestData.RequestTimeout
53-
};
54-
55-
client.DefaultRequestHeaders.ExpectContinue = false;
56-
57-
this._clients.TryAdd(hashCode, client);
58-
return client;
48+
client = this._clients.GetOrAdd(key, h =>
49+
{
50+
var handler = CreateHttpClientHandler(requestData);
51+
var httpClient = new HttpClient(handler, false)
52+
{
53+
Timeout = requestData.RequestTimeout
54+
};
55+
56+
httpClient.DefaultRequestHeaders.ExpectContinue = false;
57+
return httpClient;
58+
});
59+
}
5960
}
6061

62+
return client;
6163
}
6264

6365
public virtual ElasticsearchResponse<TReturn> Request<TReturn>(RequestData requestData) where TReturn : class
@@ -192,7 +194,6 @@ protected virtual void SetBasicAuthenticationIfNeeded(HttpRequestMessage request
192194
}
193195
}
194196

195-
196197
private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMethod)
197198
{
198199
switch (httpMethod)
@@ -207,6 +208,20 @@ private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMetho
207208
}
208209
}
209210

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

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

0 commit comments

Comments
 (0)