-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathConnectionConfiguration.cs
235 lines (210 loc) · 7.93 KB
/
ConnectionConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Elasticsearch.Net.ConnectionPool;
using Elasticsearch.Net.Serialization;
namespace Elasticsearch.Net.Connection
{
public class ConnectionConfiguration :
ConnectionConfiguration<ConnectionConfiguration>,
IConnectionConfiguration<ConnectionConfiguration>
{
public ConnectionConfiguration(Uri uri = null)
: base(uri)
{
}
public ConnectionConfiguration(IConnectionPool connectionPool)
: base(connectionPool)
{
}
}
public class ConnectionConfiguration<T> : IConnectionConfigurationValues
where T : ConnectionConfiguration<T>
{
private IConnectionPool _connectionPool;
IConnectionPool IConnectionConfigurationValues.ConnectionPool { get { return _connectionPool; } }
private int _timeout;
int IConnectionConfigurationValues.Timeout { get { return _timeout; }}
private int? _pingTimeout;
int? IConnectionConfigurationValues.PingTimeout { get{ return _pingTimeout; } }
private int? _deadTimeout;
int? IConnectionConfigurationValues.DeadTimeout { get{ return _deadTimeout; } }
private int? _maxDeadTimeout;
int? IConnectionConfigurationValues.MaxDeadTimeout { get{ return _maxDeadTimeout; } }
private string _proxyUsername;
string IConnectionConfigurationValues.ProxyUsername { get{ return _proxyUsername; } }
private string _proxyPassword;
string IConnectionConfigurationValues.ProxyPassword { get{ return _proxyPassword; } }
private bool _disablePings;
bool IConnectionConfigurationValues.DisablePings { get{ return _disablePings; } }
private string _proxyAddress;
string IConnectionConfigurationValues.ProxyAddress { get{ return _proxyAddress; } }
private int _maximumAsyncConnections;
int IConnectionConfigurationValues.MaximumAsyncConnections { get{ return _maximumAsyncConnections; } }
private int? _maxRetries;
int? IConnectionConfigurationValues.MaxRetries { get{ return _maxRetries; } }
private bool _usePrettyResponses;
bool IConnectionConfigurationValues.UsesPrettyResponses { get{ return _usePrettyResponses; } }
private bool _sniffOnStartup;
bool IConnectionConfigurationValues.SniffsOnStartup { get{ return _sniffOnStartup; } }
private bool _sniffOnConectionFault;
bool IConnectionConfigurationValues.SniffsOnConnectionFault { get{ return _sniffOnConectionFault; } }
private TimeSpan? _sniffLifeSpan;
TimeSpan? IConnectionConfigurationValues.SniffInformationLifeSpan { get{ return _sniffLifeSpan; } }
private bool _traceEnabled;
bool IConnectionConfigurationValues.TraceEnabled { get{ return _traceEnabled; } }
private Action<ElasticsearchResponse> _connectionStatusHandler;
Action<ElasticsearchResponse> IConnectionConfigurationValues.ConnectionStatusHandler { get{ return _connectionStatusHandler; } }
private NameValueCollection _queryString;
NameValueCollection IConnectionConfigurationValues.QueryStringParameters { get{ return _queryString; } }
IElasticsearchSerializer IConnectionConfigurationValues.Serializer { get; set; }
public ConnectionConfiguration(IConnectionPool connectionPool)
{
this._timeout = 60*1000;
//this.UriSpecifiedBasicAuth = !uri.UserInfo.IsNullOrEmpty();
//this.Uri = uri;
this._connectionStatusHandler = this.ConnectionStatusDefaultHandler;
this._maximumAsyncConnections = 0;
this._connectionPool = connectionPool;
}
public ConnectionConfiguration(Uri uri = null)
: this(new SingleNodeConnectionPool(uri ?? new Uri("http://localhost:9200")))
{
//this.Host = uri.Host;
//this.Port = uri.Port;
}
public T MaximumRetries(int maxRetries)
{
this._maxRetries = maxRetries;
return (T) this;
}
public T SniffOnConnectionFault(bool sniffsOnConnectionFault = true)
{
this._sniffOnConectionFault = sniffsOnConnectionFault;
return (T)this;
}
public T SniffOnStartup(bool sniffsOnStartup = true)
{
this._sniffOnStartup = sniffsOnStartup;
return (T)this;
}
public T SniffLifeSpan(TimeSpan sniffTimeSpan)
{
this._sniffLifeSpan = sniffTimeSpan;
return (T)this;
}
/// <summary>
/// Enable Trace signals to the IConnection that it should put debug information on the Trace.
/// </summary>
public T EnableTrace(bool enabled = true)
{
this._traceEnabled = enabled;
return (T) this;
}
/// <summary>
/// When a node is used for the very first time or when it's used for the first time after it has been marked dead
/// a ping with a very low timeout is send to the node to make sure that when it's still dead it reports it as fast as possible.
/// You can disable these pings globally here if you rather have it fail on the possible slower original request
/// </summary>
public T DisablePing(bool disable = true)
{
this._disablePings = disable;
return (T) this;
}
/// <summary>
/// This NameValueCollection will be appended to every url NEST calls, great if you need to pass i.e an API key.
/// </summary>
public T SetGlobalQueryStringParameters(NameValueCollection queryStringParameters)
{
if (this._queryString != null)
{
this._queryString.Add(queryStringParameters);
}
this._queryString = queryStringParameters;
return (T) this;
}
/// <summary>
/// Timeout in milliseconds when the .NET webrequest should abort the request, note that you can set this to a high value here,
/// and specify the timeout in various calls on Elasticsearch's side.
/// </summary>
/// <param name="timeout">time out in milliseconds</param>
public T SetTimeout(int timeout)
{
this._timeout = timeout;
return (T) this;
}
/// <summary>
/// This is a separate timeout for Ping() requests. A ping should fail as fast as possible.
/// </summary>
/// <param name="timeout">The ping timeout in milliseconds defaults to 50</param>
public T SetPingTimeout(int timeout)
{
this._pingTimeout = timeout;
return (T) this;
}
/// <summary>
/// Sets the default dead timeout factor when a node has been marked dead.
/// </summary>
/// <remarks>Some connection pools may use a flat timeout whilst others take this factor and increase it exponentially</remarks>
/// <param name="timeout"></param>
public T SetDeadTimeout(int timeout)
{
this._deadTimeout = timeout;
return (T) this;
}
/// <summary>
/// Sets the maximum time a node can be marked dead.
/// Different implementations of IConnectionPool may choose a different default.
/// </summary>
/// <param name="timeout">The timeout in milliseconds</param>
public T SetMaxDeadTimeout(int timeout)
{
this._maxDeadTimeout = timeout;
return (T) this;
}
/// <summary>
/// Semaphore asynchronous connections automatically by giving
/// it a maximum concurrent connections.
/// </summary>
/// <param name="maximum">defaults to 0 (unbounded)</param>
public T SetMaximumAsyncConnections(int maximum)
{
this._maximumAsyncConnections = maximum;
return (T) this;
}
/// <summary>
/// If your connection has to go through proxy use this method to specify the proxy url
/// </summary>
public T SetProxy(Uri proxyAdress, string username, string password)
{
proxyAdress.ThrowIfNull("proxyAdress");
this._proxyAddress = proxyAdress.ToString();
this._proxyUsername = username;
this._proxyPassword = password;
return (T) this;
}
/// <summary>
/// Append ?pretty=true to requests, this helps to debug send and received json.
/// </summary>
public T UsePrettyResponses(bool b = true)
{
this._usePrettyResponses = b;
this.SetGlobalQueryStringParameters(new NameValueCollection {{"pretty", b.ToString().ToLowerInvariant()}});
return (T) this;
}
protected void ConnectionStatusDefaultHandler(ElasticsearchResponse status)
{
return;
}
/// <summary>
/// Global callback for every response that NEST receives, useful for custom logging.
/// </summary>
public T SetConnectionStatusHandler(Action<ElasticsearchResponse> handler)
{
handler.ThrowIfNull("handler");
this._connectionStatusHandler = handler;
return (T) this;
}
}
}