Skip to content

Commit 8cf600b

Browse files
committed
Merge pull request #1260 from elasticsearch/feature/configurable-tcp-keep-alive
expose SetTcpKeepAlive() on ConnectionSettings
2 parents a5b262d + e7d99ed commit 8cf600b

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/Connections/Elasticsearch.Net.Connection.HttpClient/HttpClientConnection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Elasticsearch.Net.Connection
1212
/// <summary>
1313
/// IConnection implemented using <see cref="System.Net.Http.HttpClient"/>
1414
/// </summary>
15+
[Obsolete("The HttpClientConnection uses HttpClient and is not currently fully tested, with Elasticsearch.NET and NEST 2.0 we'll move the default HttpConnection over the HttpClient")]
1516
public class HttpClientConnection : IConnection, IDisposable
1617
{
1718
private readonly IConnectionConfigurationValues _settings;

src/Elasticsearch.Net/Connection/Configuration/ConnectionConfiguration.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public class ConnectionConfiguration<T> : IConnectionConfigurationValues, IHideO
6767
private TimeSpan? _maxRetryTimeout;
6868
TimeSpan? IConnectionConfigurationValues.MaxRetryTimeout { get{ return _maxRetryTimeout; } }
6969

70+
private int? _keepAliveTime;
71+
int? IConnectionConfigurationValues.KeepAliveTime { get{ return _keepAliveTime; } }
72+
73+
private int? _keepAliveInterval;
74+
int? IConnectionConfigurationValues.KeepAliveInterval { get{ return _keepAliveInterval; } }
75+
7076
private string _proxyUsername;
7177
string IConnectionConfigurationValues.ProxyUsername { get{ return _proxyUsername; } }
7278

@@ -156,6 +162,13 @@ public ConnectionConfiguration(Uri uri = null)
156162
//this.Port = uri.Port
157163
}
158164

165+
public T EnableTcpKeepAlive(int keepAliveTime, int keepAliveInterval)
166+
{
167+
this._keepAliveTime = keepAliveTime;
168+
this._keepAliveInterval = keepAliveInterval;
169+
return (T) this;
170+
}
171+
159172
public T MaximumRetries(int maxRetries)
160173
{
161174
this._maxRetries = maxRetries;

src/Elasticsearch.Net/Connection/Configuration/IConnectionConfigurationValues.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ public interface IConnectionConfigurationValues
162162
/// Basic access authorization credentials to specify with all requests.
163163
/// </summary>
164164
/// TODO: Rename to BasicAuthenticationCredentials in 2.0
165-
BasicAuthorizationCredentials BasicAuthorizationCredentials { get; }
165+
BasicAuthorizationCredentials BasicAuthorizationCredentials { get; }
166+
167+
/// <summary>
168+
/// KeepAliveTime - specifies the timeout, in milliseconds, with no
169+
/// activity until the first keep-alive packet is sent.
170+
/// </summary>
171+
int? KeepAliveTime { get; }
172+
173+
/// <summary>
174+
/// KeepAliveInterval - specifies the interval, in milliseconds, between
175+
/// when successive keep-alive packets are sent if no acknowledgement is
176+
/// received.
177+
/// </summary>
178+
int? KeepAliveInterval { get; }
166179
}
167180
}

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ protected virtual void AlterServicePoint(ServicePoint requestServicePoint)
148148
requestServicePoint.UseNagleAlgorithm = false;
149149
requestServicePoint.Expect100Continue = false;
150150
requestServicePoint.ConnectionLimit = 10000;
151+
//looking at http://referencesource.microsoft.com/#System/net/System/Net/ServicePoint.cs
152+
//this method only sets internal values and wont actually cause timers and such to be reset
153+
//So it should be idempotent if called with the same parameters
154+
if (this.ConnectionSettings.KeepAliveTime.HasValue && this.ConnectionSettings.KeepAliveInterval.HasValue)
155+
requestServicePoint.SetTcpKeepAlive(true, this.ConnectionSettings.KeepAliveTime.Value, this.ConnectionSettings.KeepAliveInterval.Value);
151156
}
152157

153158
protected virtual HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)

0 commit comments

Comments
 (0)