18
18
namespace Elasticsearch . Net
19
19
{
20
20
/// <summary>
21
- /// Allows you to control how <see cref="ElasticLowLevelClient"/> behaves and where/how it connects to Elasticsearch
21
+ /// Allows you to control how <see cref="ElasticLowLevelClient" /> behaves and where/how it connects to Elasticsearch
22
22
/// </summary>
23
23
public class ConnectionConfiguration : ConnectionConfiguration < ConnectionConfiguration >
24
24
{
@@ -42,33 +42,42 @@ public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfigu
42
42
/// <summary>
43
43
/// The default connection limit for both Elasticsearch.Net and Nest. Defaults to <c>80</c> except for
44
44
/// HttpClientHandler implementations based on curl, which defaults to
45
- /// <see cref="Environment.ProcessorCount"/>
45
+ /// <see cref="Environment.ProcessorCount" />
46
46
/// </summary>
47
47
public static readonly int DefaultConnectionLimit = IsCurlHandler ? Environment . ProcessorCount : 80 ;
48
48
49
+
49
50
/// <summary>
50
51
/// The default user agent for Elasticsearch.Net
51
52
/// </summary>
52
- public static readonly string DefaultUserAgent = $ "elasticsearch-net/{ typeof ( IConnectionConfigurationValues ) . Assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) . InformationalVersion } ({ RuntimeInformation . OSDescription } ; { RuntimeInformation . FrameworkDescription } ; Elasticsearch.Net)";
53
+ public static readonly string DefaultUserAgent =
54
+ $ "elasticsearch-net/{ typeof ( IConnectionConfigurationValues ) . Assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) . InformationalVersion } ({ RuntimeInformation . OSDescription } ; { RuntimeInformation . FrameworkDescription } ; Elasticsearch.Net)";
53
55
54
56
/// <summary>
55
- /// Creates a new instance of <see cref="ConnectionConfiguration"/>
57
+ /// Creates a new instance of <see cref="ConnectionConfiguration" />
56
58
/// </summary>
57
59
/// <param name="uri">The root of the Elasticsearch node we want to connect to. Defaults to http://localhost:9200</param>
58
60
[ SuppressMessage ( "Microsoft.Reliability" , "CA2000:Dispose objects before losing scope" ) ]
59
61
public ConnectionConfiguration ( Uri uri = null )
60
62
: this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) ) { }
61
63
62
64
/// <summary>
63
- /// Creates a new instance of <see cref="ConnectionConfiguration"/>
65
+ /// Sets up the client to communicate to Elastic Cloud using <paramref name="cloudId" />,
66
+ /// <para><see cref="CloudConnectionPool" /> documentation for more information on how to obtain your Cloud Id</para>
67
+ /// </summary>
68
+ public ConnectionConfiguration ( string cloudId , BasicAuthenticationCredentials credentials ) : this (
69
+ new CloudConnectionPool ( cloudId , credentials ) ) { }
70
+
71
+ /// <summary>
72
+ /// Creates a new instance of <see cref="ConnectionConfiguration" />
64
73
/// </summary>
65
74
/// <param name="connectionPool">A connection pool implementation that tells the client what nodes are available</param>
66
75
public ConnectionConfiguration ( IConnectionPool connectionPool )
67
76
// ReSharper disable once IntroduceOptionalParameters.Global
68
77
: this ( connectionPool , null , null ) { }
69
78
70
79
/// <summary>
71
- /// Creates a new instance of <see cref="ConnectionConfiguration"/>
80
+ /// Creates a new instance of <see cref="ConnectionConfiguration" />
72
81
/// </summary>
73
82
/// <param name="connectionPool">A connection pool implementation that tells the client what nodes are available</param>
74
83
/// <param name="connection">An connection implementation that can make API requests</param>
@@ -77,15 +86,15 @@ public ConnectionConfiguration(IConnectionPool connectionPool, IConnection conne
77
86
: this ( connectionPool , connection , null ) { }
78
87
79
88
/// <summary>
80
- /// Creates a new instance of <see cref="ConnectionConfiguration"/>
89
+ /// Creates a new instance of <see cref="ConnectionConfiguration" />
81
90
/// </summary>
82
91
/// <param name="connectionPool">A connection pool implementation that tells the client what nodes are available</param>
83
92
/// <param name="serializer">A serializer implementation used to serialize requests and deserialize responses</param>
84
93
public ConnectionConfiguration ( IConnectionPool connectionPool , IElasticsearchSerializer serializer )
85
94
: this ( connectionPool , null , serializer ) { }
86
95
87
96
/// <summary>
88
- /// Creates a new instance of <see cref="ConnectionConfiguration"/>
97
+ /// Creates a new instance of <see cref="ConnectionConfiguration" />
89
98
/// </summary>
90
99
/// <param name="connectionPool">A connection pool implementation that tells the client what nodes are available</param>
91
100
/// <param name="connection">An connection implementation that can make API requests</param>
@@ -95,7 +104,7 @@ public ConnectionConfiguration(IConnectionPool connectionPool, IConnection conne
95
104
96
105
internal static bool IsCurlHandler { get ; } =
97
106
#if DOTNETCORE
98
- typeof ( HttpClientHandler ) . Assembly . GetType ( "System.Net.Http.CurlHandler" ) != null ;
107
+ typeof ( HttpClientHandler ) . Assembly . GetType ( "System.Net.Http.CurlHandler" ) != null ;
99
108
#else
100
109
false ;
101
110
#endif
@@ -107,72 +116,45 @@ public abstract class ConnectionConfiguration<T> : IConnectionConfigurationValue
107
116
where T : ConnectionConfiguration < T >
108
117
{
109
118
private readonly IConnection _connection ;
110
-
111
119
private readonly IConnectionPool _connectionPool ;
112
-
113
120
private readonly NameValueCollection _headers = new NameValueCollection ( ) ;
114
-
115
121
private readonly NameValueCollection _queryString = new NameValueCollection ( ) ;
116
122
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim ( 1 , 1 ) ;
123
+ private readonly ElasticsearchUrlFormatter _urlFormatter ;
117
124
118
125
private BasicAuthenticationCredentials _basicAuthCredentials ;
119
-
120
126
private X509CertificateCollection _clientCertificates ;
121
127
private Action < IApiCallDetails > _completedRequestHandler = DefaultCompletedRequestHandler ;
122
-
123
128
private int _connectionLimit ;
124
-
125
129
private TimeSpan ? _deadTimeout ;
126
130
127
131
private bool _disableAutomaticProxyDetection = false ;
128
132
129
133
private bool _disableDirectStreaming = false ;
130
-
131
134
private bool _disablePings ;
132
-
133
135
private bool _enableHttpCompression ;
134
-
135
136
private bool _enableHttpPipelining = true ;
136
-
137
137
private TimeSpan ? _keepAliveInterval ;
138
-
139
138
private TimeSpan ? _keepAliveTime ;
140
-
141
139
private TimeSpan ? _maxDeadTimeout ;
142
-
143
140
private int ? _maxRetries ;
144
-
145
141
private TimeSpan ? _maxRetryTimeout ;
146
-
147
142
private Func < Node , bool > _nodePredicate = DefaultNodePredicate ;
148
143
private Action < RequestData > _onRequestDataCreated = DefaultRequestDataCreated ;
149
-
150
144
private TimeSpan ? _pingTimeout ;
151
-
152
145
private bool _prettyJson ;
153
-
154
146
private string _proxyAddress ;
155
147
156
148
private string _proxyPassword ;
157
-
158
149
private string _proxyUsername ;
159
-
160
150
private TimeSpan _requestTimeout ;
161
-
162
151
private Func < object , X509Certificate , X509Chain , SslPolicyErrors , bool > _serverCertificateValidationCallback ;
163
-
164
152
private IReadOnlyCollection < int > _skipDeserializationForStatusCodes = new ReadOnlyCollection < int > ( new int [ ] { } ) ;
165
-
166
153
private TimeSpan ? _sniffLifeSpan ;
167
-
168
154
private bool _sniffOnConnectionFault ;
169
-
170
155
private bool _sniffOnStartup ;
171
-
172
156
private bool _throwExceptions ;
173
157
174
- private readonly ElasticsearchUrlFormatter _urlFormatter ;
175
-
176
158
private string _userAgent = ConnectionConfiguration . DefaultUserAgent ;
177
159
178
160
protected ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection , IElasticsearchSerializer requestResponseSerializer )
@@ -190,6 +172,12 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
190
172
_nodePredicate = DefaultReseedableNodePredicate ;
191
173
192
174
_urlFormatter = new ElasticsearchUrlFormatter ( this ) ;
175
+
176
+ if ( connectionPool is CloudConnectionPool cloudPool )
177
+ {
178
+ _basicAuthCredentials = cloudPool . BasicCredentials ;
179
+ _enableHttpCompression = true ;
180
+ }
193
181
}
194
182
195
183
protected IElasticsearchSerializer UseThisRequestResponseSerializer { get ; set ; }
@@ -279,15 +267,20 @@ public T EnableTcpKeepAlive(TimeSpan keepAliveTime, TimeSpan keepAliveInterval)
279
267
/// <summary>
280
268
/// Limits the number of concurrent connections that can be opened to an endpoint. Defaults to <c>80</c>.
281
269
/// <para>
282
- /// For Desktop CLR, this setting applies to the DefaultConnectionLimit property on the ServicePointManager object when creating
270
+ /// For Desktop CLR, this setting applies to the DefaultConnectionLimit property on the ServicePointManager object when
271
+ /// creating
283
272
/// ServicePoint objects, affecting the default <see cref="IConnection" /> implementation.
284
273
/// </para>
285
274
/// <para>
286
- /// For Core CLR, this setting applies to the MaxConnectionsPerServer property on the HttpClientHandler instances used by the HttpClient
275
+ /// For Core CLR, this setting applies to the MaxConnectionsPerServer property on the HttpClientHandler instances used by
276
+ /// the HttpClient
287
277
/// inside the default <see cref="IConnection" /> implementation
288
278
/// </para>
289
279
/// </summary>
290
- /// <param name="connectionLimit">The connection limit, a value lower then 0 will cause the connection limit not to be set at all</param>
280
+ /// <param name="connectionLimit">
281
+ /// The connection limit, a value lower then 0 will cause the connection limit not to be set
282
+ /// at all
283
+ /// </param>
291
284
public T ConnectionLimit ( int connectionLimit ) => Assign ( connectionLimit , ( a , v ) => a . _connectionLimit = v ) ;
292
285
293
286
/// <summary>
@@ -305,7 +298,8 @@ public T SniffOnConnectionFault(bool sniffsOnConnectionFault = true) =>
305
298
/// Set the duration after which a cluster state is considered stale and a sniff should be performed again.
306
299
/// An <see cref="IConnectionPool" /> has to signal it supports reseeding, otherwise sniffing will never happen.
307
300
/// Defaults to 1 hour.
308
- /// Set to null to disable completely. Sniffing will only ever happen on ConnectionPools that return true for SupportsReseeding
301
+ /// Set to null to disable completely. Sniffing will only ever happen on ConnectionPools that return true for
302
+ /// SupportsReseeding
309
303
/// </summary>
310
304
/// <param name="sniffLifeSpan">The duration a clusterstate is considered fresh, set to null to disable periodic sniffing</param>
311
305
public T SniffLifeSpan ( TimeSpan ? sniffLifeSpan ) => Assign ( sniffLifeSpan , ( a , v ) => a . _sniffLifeSpan = v ) ;
@@ -331,19 +325,23 @@ public T SniffOnConnectionFault(bool sniffsOnConnectionFault = true) =>
331
325
332
326
/// <summary>
333
327
/// 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
334
- /// 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.
328
+ /// 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
329
+ /// possible.
335
330
/// You can disable these pings globally here if you rather have it fail on the possible slower original request
336
331
/// </summary>
337
332
public T DisablePing ( bool disable = true ) => Assign ( disable , ( a , v ) => a . _disablePings = v ) ;
338
333
339
334
/// <summary>
340
- /// A collection of query string parameters that will be sent with every request. Useful in situations where you always need to pass a
335
+ /// A collection of query string parameters that will be sent with every request. Useful in situations where you always
336
+ /// need to pass a
341
337
/// parameter e.g. an API key.
342
338
/// </summary>
343
- public T GlobalQueryStringParameters ( NameValueCollection queryStringParameters ) => Assign ( queryStringParameters , ( a , v ) => a . _queryString . Add ( v ) ) ;
339
+ public T GlobalQueryStringParameters ( NameValueCollection queryStringParameters ) =>
340
+ Assign ( queryStringParameters , ( a , v ) => a . _queryString . Add ( v ) ) ;
344
341
345
342
/// <summary>
346
- /// A collection of headers that will be sent with every request. Useful in situations where you always need to pass a header e.g. a custom
343
+ /// A collection of headers that will be sent with every request. Useful in situations where you always need to pass a
344
+ /// header e.g. a custom
347
345
/// auth header
348
346
/// </summary>
349
347
public T GlobalHeaders ( NameValueCollection headers ) => Assign ( headers , ( a , v ) => a . _headers . Add ( v ) ) ;
@@ -460,11 +458,14 @@ public T BasicAuthentication(string userName, string password) =>
460
458
public T EnableHttpPipelining ( bool enabled = true ) => Assign ( enabled , ( a , v ) => a . _enableHttpPipelining = v ) ;
461
459
462
460
/// <summary>
463
- /// Register a predicate to select which nodes that you want to execute API calls on. Note that sniffing requests omit this predicate and
461
+ /// Register a predicate to select which nodes that you want to execute API calls on. Note that sniffing requests omit this
462
+ /// predicate and
464
463
/// always execute on all nodes.
465
- /// When using an <see cref="IConnectionPool" /> implementation that supports reseeding of nodes, this will default to omitting master only
464
+ /// When using an <see cref="IConnectionPool" /> implementation that supports reseeding of nodes, this will default to
465
+ /// omitting master only
466
466
/// node from regular API calls.
467
- /// When using static or single node connection pooling it is assumed the list of node you instantiate the client with should be taken
467
+ /// When using static or single node connection pooling it is assumed the list of node you instantiate the client with
468
+ /// should be taken
468
469
/// verbatim.
469
470
/// </summary>
470
471
/// <param name="predicate">Return true if you want the node to be used for API calls</param>
@@ -478,7 +479,8 @@ public T NodePredicate(Func<Node, bool> predicate)
478
479
479
480
/// <summary>
480
481
/// Turns on settings that aid in debugging like DisableDirectStreaming() and PrettyJson()
481
- /// so that the original request and response JSON can be inspected. It also always asks the server for the full stack trace on errors
482
+ /// so that the original request and response JSON can be inspected. It also always asks the server for the full stack
483
+ /// trace on errors
482
484
/// </summary>
483
485
/// <param name="onRequestCompleted">
484
486
/// An optional callback to be performed when the request completes. This will
@@ -519,19 +521,22 @@ public T ClientCertificates(X509CertificateCollection certificates) =>
519
521
Assign ( certificates , ( a , v ) => a . _clientCertificates = v ) ;
520
522
521
523
/// <summary>
522
- /// Use a <see cref="System.Security.Cryptography.X509Certificates.X509Certificate"/> to authenticate all HTTP requests. You can also set them on individual request using <see cref="RequestConfiguration.ClientCertificates" />
524
+ /// Use a <see cref="System.Security.Cryptography.X509Certificates.X509Certificate" /> to authenticate all HTTP requests.
525
+ /// You can also set them on individual request using <see cref="RequestConfiguration.ClientCertificates" />
523
526
/// </summary>
524
527
public T ClientCertificate ( X509Certificate certificate ) =>
525
528
Assign ( new X509Certificate2Collection { certificate } , ( a , v ) => a . _clientCertificates = v ) ;
526
529
527
530
/// <summary>
528
- /// Use a file path to a certificate to authenticate all HTTP requests. You can also set them on individual request using <see cref="RequestConfiguration.ClientCertificates" />
531
+ /// Use a file path to a certificate to authenticate all HTTP requests. You can also set them on individual request using
532
+ /// <see cref="RequestConfiguration.ClientCertificates" />
529
533
/// </summary>
530
534
public T ClientCertificate ( string certificatePath ) =>
531
535
Assign ( new X509Certificate2Collection { new X509Certificate ( certificatePath ) } , ( a , v ) => a . _clientCertificates = v ) ;
532
536
533
537
/// <summary>
534
- /// Configure the client to skip deserialization of certain status codes e.g: you run elasticsearch behind a proxy that returns a HTML for 401,
538
+ /// Configure the client to skip deserialization of certain status codes e.g: you run elasticsearch behind a proxy that
539
+ /// returns a HTML for 401,
535
540
/// 500
536
541
/// </summary>
537
542
public T SkipDeserializationForStatusCodes ( params int [ ] statusCodes ) =>
0 commit comments