3
3
using System . ComponentModel ;
4
4
5
5
namespace Elasticsearch . Net
6
- {
6
+ {
7
7
/// <summary>
8
8
/// ConnectionConfiguration allows you to control how ElasticsearchClient behaves and where/how it connects
9
9
/// to elasticsearch
@@ -12,40 +12,40 @@ public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfigu
12
12
{
13
13
public static TimeSpan DefaultTimeout = TimeSpan . FromMinutes ( 1 ) ;
14
14
public static TimeSpan DefaultPingTimeout = TimeSpan . FromSeconds ( 2 ) ;
15
- public static TimeSpan DefaultPingTimeoutOnSSL = TimeSpan . FromSeconds ( 5 ) ;
16
-
15
+ public static TimeSpan DefaultPingTimeoutOnSSL = TimeSpan . FromSeconds ( 5 ) ;
16
+
17
17
/// <summary>
18
18
/// ConnectionConfiguration allows you to control how ElasticsearchClient behaves and where/how it connects
19
19
/// to elasticsearch
20
20
/// </summary>
21
21
/// <param name="uri">The root of the elasticsearch node we want to connect to. Defaults to http://localhost:9200</param>
22
22
public ConnectionConfiguration ( Uri uri = null )
23
- : this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) )
24
- { }
25
-
23
+ : this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) )
24
+ { }
25
+
26
26
/// <summary>
27
27
/// ConnectionConfiguration allows you to control how ElasticsearchClient behaves and where/how it connects
28
28
/// to elasticsearch
29
29
/// </summary>
30
30
/// <param name="connectionPool">A connection pool implementation that'll tell the client what nodes are available</param>
31
- public ConnectionConfiguration ( IConnectionPool connectionPool )
32
- // ReSharper disable once IntroduceOptionalParameters.Global
33
- : this ( connectionPool , null , null )
31
+ public ConnectionConfiguration ( IConnectionPool connectionPool )
32
+ // ReSharper disable once IntroduceOptionalParameters.Global
33
+ : this ( connectionPool , null , null )
34
34
{ }
35
35
36
- public ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection )
37
- // ReSharper disable once IntroduceOptionalParameters.Global
38
- : this ( connectionPool , connection , null )
36
+ public ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection )
37
+ // ReSharper disable once IntroduceOptionalParameters.Global
38
+ : this ( connectionPool , connection , null )
39
39
{ }
40
40
41
- public ConnectionConfiguration ( IConnectionPool connectionPool , IElasticsearchSerializer serializer )
42
- : this ( connectionPool , null , serializer )
43
- { }
44
-
45
- // ReSharper disable once MemberCanBePrivate.Global
46
- // eventhough we use don't use this we very much would like to expose this constructor
47
- public ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection , IElasticsearchSerializer serializer )
48
- : base ( connectionPool , connection , serializer )
41
+ public ConnectionConfiguration ( IConnectionPool connectionPool , Func < ConnectionConfiguration , IElasticsearchSerializer > serializerFactory )
42
+ : this ( connectionPool , null , serializerFactory )
43
+ { }
44
+
45
+ // ReSharper disable once MemberCanBePrivate.Global
46
+ // eventhough we use don't use this we very much would like to expose this constructor
47
+ public ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection , Func < ConnectionConfiguration , IElasticsearchSerializer > serializerFactory )
48
+ : base ( connectionPool , connection , serializerFactory )
49
49
{ }
50
50
}
51
51
@@ -130,24 +130,26 @@ private static void DefaultApiCallHandler(IApiCallDetails status) { }
130
130
BasicAuthenticationCredentials _basicAuthCredentials ;
131
131
BasicAuthenticationCredentials IConnectionConfigurationValues . BasicAuthenticationCredentials => _basicAuthCredentials ;
132
132
133
- private readonly IElasticsearchSerializer _serializer ;
133
+ protected IElasticsearchSerializer _serializer ;
134
134
IElasticsearchSerializer IConnectionConfigurationValues . Serializer => _serializer ;
135
135
136
136
private readonly IConnectionPool _connectionPool ;
137
+ private readonly Func < T , IElasticsearchSerializer > _serializerFactory ;
137
138
IConnectionPool IConnectionConfigurationValues . ConnectionPool => _connectionPool ;
138
139
139
140
private readonly IConnection _connection ;
140
141
IConnection IConnectionConfigurationValues . Connection => _connection ;
141
142
142
143
[ System . Diagnostics . CodeAnalysis . SuppressMessage (
143
- "Potential Code Quality Issues" , "RECS0021:Warns about calls to virtual member functions occuring in the constructor" ,
144
+ "Potential Code Quality Issues" , "RECS0021:Warns about calls to virtual member functions occuring in the constructor" ,
144
145
Justification = "We want the virtual method to run on most derived" ) ]
145
- protected ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection , IElasticsearchSerializer serializer )
146
+ protected ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection , Func < T , IElasticsearchSerializer > serializerFactory )
146
147
{
147
148
this . _connectionPool = connectionPool ;
148
- this . _connection = connection ?? new HttpConnection ( ) ;
149
- // ReSharper disable once VirtualMemberCallInContructor
150
- this . _serializer = serializer ?? this . DefaultSerializer ( ) ;
149
+ this . _connection = connection ?? new HttpConnection ( ) ;
150
+ this . _serializerFactory = serializerFactory ?? ( c=> this . DefaultSerializer ( ( T ) this ) ) ;
151
+ // ReSharper disable once VirtualMemberCallInContructor
152
+ this . _serializer = this . _serializerFactory ( ( T ) this ) ;
151
153
152
154
this . _requestTimeout = ConnectionConfiguration . DefaultTimeout ;
153
155
this . _sniffOnConnectionFault = true ;
@@ -157,100 +159,100 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
157
159
158
160
T Assign ( Action < ConnectionConfiguration < T > > assigner ) => Fluent . Assign ( ( T ) this , assigner ) ;
159
161
160
- protected virtual IElasticsearchSerializer DefaultSerializer ( ) => new ElasticsearchDefaultSerializer ( ) ;
162
+ protected virtual IElasticsearchSerializer DefaultSerializer ( T settings ) => new ElasticsearchDefaultSerializer ( ) ;
161
163
162
164
public T EnableTcpKeepAlive ( TimeSpan keepAliveTime , TimeSpan keepAliveInterval ) =>
163
165
Assign ( a => { this . _keepAliveTime = keepAliveTime ; this . _keepAliveInterval = keepAliveInterval ; } ) ;
164
166
165
- public T MaximumRetries ( int maxRetries ) => Assign ( a => a . _maxRetries = maxRetries ) ;
166
-
167
+ public T MaximumRetries ( int maxRetries ) => Assign ( a => a . _maxRetries = maxRetries ) ;
168
+
167
169
/// <summary>
168
170
/// On connection pools that support reseeding setting this to true (default) will resniff the cluster when a call fails
169
171
/// </summary>
170
- public T SniffOnConnectionFault ( bool sniffsOnConnectionFault = true ) => Assign ( a => a . _sniffOnConnectionFault = sniffsOnConnectionFault ) ;
171
-
172
+ public T SniffOnConnectionFault ( bool sniffsOnConnectionFault = true ) => Assign ( a => a . _sniffOnConnectionFault = sniffsOnConnectionFault ) ;
173
+
172
174
/// <summary>
173
175
/// Enables sniffing on first usage of a connection pool if that pool supports reseeding, defaults to true
174
176
/// </summary>
175
- public T SniffOnStartup ( bool sniffsOnStartup = true ) => Assign ( a => a . _sniffOnStartup = sniffsOnStartup ) ;
176
-
177
+ public T SniffOnStartup ( bool sniffsOnStartup = true ) => Assign ( a => a . _sniffOnStartup = sniffsOnStartup ) ;
178
+
177
179
/// <summary>
178
180
/// Set the duration after which a cluster state is considered stale and a sniff should be performed again.
179
181
/// An IConnectionPool has to signal it supports reseeding otherwise sniffing will never happen.
180
182
/// Defaults to 1 hour.
181
183
/// Set to null to disable completely. Sniffing will only ever happen on ConnectionPools that return true for SupportsReseeding
182
184
/// </summary>
183
185
/// <param name="sniffLifeSpan">The duration a clusterstate is considered fresh, set to null to disable periodic sniffing</param>
184
- public T SniffLifeSpan ( TimeSpan ? sniffLifeSpan ) => Assign ( a => a . _sniffLifeSpan = sniffLifeSpan ) ;
185
-
186
+ public T SniffLifeSpan ( TimeSpan ? sniffLifeSpan ) => Assign ( a => a . _sniffLifeSpan = sniffLifeSpan ) ;
187
+
186
188
/// <summary>
187
189
/// Enable gzip compressed requests and responses, do note that you need to configure elasticsearch to set this
188
190
/// <para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html"</para>
189
191
/// </summary>
190
192
public T EnableHttpCompression ( bool enabled = true ) => Assign ( a => a . _enableHttpCompression = enabled ) ;
191
193
192
- public T DisableAutomaticProxyDetection ( bool disable = true ) => Assign ( a => a . _disableAutomaticProxyDetection = disable ) ;
193
-
194
+ public T DisableAutomaticProxyDetection ( bool disable = true ) => Assign ( a => a . _disableAutomaticProxyDetection = disable ) ;
195
+
194
196
/// <summary>
195
197
/// Instead of following a c/go like error checking on response.IsValid always throw an exception
196
198
/// on the client when a call resulted in an exception on either the client or the Elasticsearch server.
197
199
/// <para>Reasons for such exceptions could be search parser errors, index missing exceptions, etc...</para>
198
200
/// </summary>
199
- public T ThrowExceptions ( bool alwaysThrow = true ) => Assign ( a => a . _throwExceptions = alwaysThrow ) ;
200
-
201
+ public T ThrowExceptions ( bool alwaysThrow = true ) => Assign ( a => a . _throwExceptions = alwaysThrow ) ;
202
+
201
203
/// <summary>
202
204
/// 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
203
205
/// 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.
204
206
/// You can disable these pings globally here if you rather have it fail on the possible slower original request
205
207
/// </summary>
206
- public T DisablePing ( bool disable = true ) => Assign ( a => a . _disablePings = disable ) ;
207
-
208
+ public T DisablePing ( bool disable = true ) => Assign ( a => a . _disablePings = disable ) ;
209
+
208
210
/// <summary>
209
211
/// This NameValueCollection will be appended to every url NEST calls, great if you need to pass i.e an API key.
210
212
/// </summary>
211
- public T GlobalQueryStringParameters ( NameValueCollection queryStringParameters ) => Assign ( a => a . _queryString . Add ( queryStringParameters ) ) ;
212
-
213
+ public T GlobalQueryStringParameters ( NameValueCollection queryStringParameters ) => Assign ( a => a . _queryString . Add ( queryStringParameters ) ) ;
214
+
213
215
/// <summary>
214
216
/// a NameValueCollection that will be send as headers for each request
215
217
/// </summary>
216
- public T GlobalHeaders ( NameValueCollection headers ) => Assign ( a => a . _headers . Add ( headers ) ) ;
217
-
218
+ public T GlobalHeaders ( NameValueCollection headers ) => Assign ( a => a . _headers . Add ( headers ) ) ;
219
+
218
220
/// <summary>
219
221
/// Sets the default timeout in milliseconds for each request to Elasticsearch.
220
222
/// NOTE: You can set this to a high value here, and specify the timeout on Elasticsearch's side.
221
223
/// </summary>
222
224
/// <param name="timeout">time out in milliseconds</param>
223
- public T RequestTimeout ( TimeSpan timeout ) => Assign ( a => a . _requestTimeout = timeout ) ;
224
-
225
+ public T RequestTimeout ( TimeSpan timeout ) => Assign ( a => a . _requestTimeout = timeout ) ;
226
+
225
227
/// <summary>
226
228
/// Sets the default ping timeout in milliseconds for ping requests, which are used
227
229
/// to determine whether a node is alive. Pings should fail as fast as possible.
228
230
/// </summary>
229
231
/// <param name="timeout">The ping timeout in milliseconds defaults to 1000, or 2000 is using SSL.</param>
230
- public T PingTimeout ( TimeSpan timeout ) => Assign ( a => a . _pingTimeout = timeout ) ;
231
-
232
+ public T PingTimeout ( TimeSpan timeout ) => Assign ( a => a . _pingTimeout = timeout ) ;
233
+
232
234
/// <summary>
233
235
/// Sets the default dead timeout factor when a node has been marked dead.
234
236
/// </summary>
235
237
/// <remarks>Some connection pools may use a flat timeout whilst others take this factor and increase it exponentially</remarks>
236
238
/// <param name="timeout"></param>
237
- public T DeadTimeout ( TimeSpan timeout ) => Assign ( a => a . _deadTimeout = timeout ) ;
238
-
239
+ public T DeadTimeout ( TimeSpan timeout ) => Assign ( a => a . _deadTimeout = timeout ) ;
240
+
239
241
/// <summary>
240
242
/// Sets the maximum time a node can be marked dead.
241
243
/// Different implementations of IConnectionPool may choose a different default.
242
244
/// </summary>
243
245
/// <param name="timeout">The timeout in milliseconds</param>
244
- public T MaxDeadTimeout ( TimeSpan timeout ) => Assign ( a => a . _maxDeadTimeout = timeout ) ;
245
-
246
+ public T MaxDeadTimeout ( TimeSpan timeout ) => Assign ( a => a . _maxDeadTimeout = timeout ) ;
247
+
246
248
/// <summary>
247
249
/// Limits the total runtime including retries separately from <see cref="RequestTimeout"/>
248
250
/// <pre>
249
251
/// When not specified defaults to <see cref="RequestTimeout"/> which itself defaults to 60seconds
250
252
/// </pre>
251
253
/// </summary>
252
- public T MaxRetryTimeout ( TimeSpan maxRetryTimeout ) => Assign ( a => a . _maxRetryTimeout = maxRetryTimeout ) ;
253
-
254
+ public T MaxRetryTimeout ( TimeSpan maxRetryTimeout ) => Assign ( a => a . _maxRetryTimeout = maxRetryTimeout ) ;
255
+
254
256
/// <summary>
255
257
/// If your connection has to go through proxy use this method to specify the proxy url
256
258
/// </summary>
@@ -261,33 +263,33 @@ public T Proxy(Uri proxyAdress, string username, string password)
261
263
this . _proxyUsername = username ;
262
264
this . _proxyPassword = password ;
263
265
return ( T ) this ;
264
- }
265
-
266
+ }
267
+
266
268
/// <summary>
267
269
/// Forces all requests to have ?pretty=true, causing elasticsearch to return formatted json.
268
270
/// Also forces the client to send out formatted json. Defaults to false
269
271
/// </summary>
270
- public T PrettyJson ( bool b = true ) => Assign ( a =>
272
+ public T PrettyJson ( bool b = true ) => Assign ( a =>
271
273
{
272
274
this . _prettyJson = b ;
273
275
if ( ! b && this . _queryString [ "pretty" ] != null ) this . _queryString . Remove ( "pretty" ) ;
274
276
else if ( b && this . _queryString [ "pretty" ] == null )
275
- this . GlobalQueryStringParameters ( new NameValueCollection { { "pretty" , b . ToString ( ) . ToLowerInvariant ( ) } } ) ;
276
- } ) ;
277
-
277
+ this . GlobalQueryStringParameters ( new NameValueCollection { { "pretty" , b . ToString ( ) . ToLowerInvariant ( ) } } ) ;
278
+ } ) ;
279
+
278
280
/// <summary>
279
281
/// Make sure the reponse bytes are always available on the ElasticsearchResponse object
280
282
/// <para>Note: that depending on the registered serializer this may cause the respond to be read in memory first</para>
281
283
/// </summary>
282
- public T DisableDirectStreaming ( bool b = true ) => Assign ( a => a . _disableDirectStreaming = b ) ;
283
-
284
+ public T DisableDirectStreaming ( bool b = true ) => Assign ( a => a . _disableDirectStreaming = b ) ;
285
+
284
286
/// <summary>
285
287
/// Global callback for every response that NEST receives, useful for custom logging.
286
288
/// Calling this multiple times will register multiple listeners
287
289
/// </summary>
288
290
public T ConnectionStatusHandler ( Action < IApiCallDetails > handler ) =>
289
- Assign ( a => a . _apiCallHandler += handler ?? DefaultApiCallHandler ) ;
290
-
291
+ Assign ( a => a . _apiCallHandler += handler ?? DefaultApiCallHandler ) ;
292
+
291
293
/// <summary>
292
294
/// Basic access authentication credentials to specify with all requests.
293
295
/// </summary>
@@ -299,8 +301,8 @@ public T BasicAuthentication(string userName, string password)
299
301
Password = password
300
302
} ;
301
303
return ( T ) this ;
302
- }
303
-
304
+ }
305
+
304
306
/// <summary>
305
307
/// Allows for requests to be pipelined. http://en.wikipedia.org/wiki/HTTP_pipelining
306
308
/// <para>Note: HTTP pipelining must also be enabled in Elasticsearch for this to work properly.</para>
0 commit comments