1
1
using System ;
2
2
using System . Collections . Specialized ;
3
3
using System . ComponentModel ;
4
+ using System . Threading ;
4
5
5
6
namespace Elasticsearch . Net
6
7
{
@@ -10,15 +11,16 @@ namespace Elasticsearch.Net
10
11
/// </summary>
11
12
public class ConnectionConfiguration : ConnectionConfiguration < ConnectionConfiguration >
12
13
{
13
- public static TimeSpan DefaultTimeout = TimeSpan . FromMinutes ( 1 ) ;
14
- public static TimeSpan DefaultPingTimeout = TimeSpan . FromSeconds ( 2 ) ;
15
- public static TimeSpan DefaultPingTimeoutOnSSL = TimeSpan . FromSeconds ( 5 ) ;
14
+ public static readonly TimeSpan DefaultTimeout = TimeSpan . FromMinutes ( 1 ) ;
15
+ public static readonly TimeSpan DefaultPingTimeout = TimeSpan . FromSeconds ( 2 ) ;
16
+ public static readonly TimeSpan DefaultPingTimeoutOnSSL = TimeSpan . FromSeconds ( 5 ) ;
16
17
17
18
/// <summary>
18
19
/// ConnectionConfiguration allows you to control how ElasticsearchClient behaves and where/how it connects
19
20
/// to elasticsearch
20
21
/// </summary>
21
22
/// <param name="uri">The root of the elasticsearch node we want to connect to. Defaults to http://localhost:9200</param>
23
+ [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Reliability" , "CA2000:Dispose objects before losing scope" ) ]
22
24
public ConnectionConfiguration ( Uri uri = null )
23
25
: this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) )
24
26
{ }
@@ -47,13 +49,17 @@ public ConnectionConfiguration(IConnectionPool connectionPool, Func<ConnectionCo
47
49
public ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection , Func < ConnectionConfiguration , IElasticsearchSerializer > serializerFactory )
48
50
: base ( connectionPool , connection , serializerFactory )
49
51
{ }
52
+
50
53
}
51
54
52
55
[ Browsable ( false ) ]
53
56
[ EditorBrowsable ( EditorBrowsableState . Never ) ]
54
57
public abstract class ConnectionConfiguration < T > : IConnectionConfigurationValues , IHideObjectMembers
55
58
where T : ConnectionConfiguration < T >
56
59
{
60
+ private SemaphoreSlim _semaphore = new SemaphoreSlim ( 1 , 1 ) ;
61
+ SemaphoreSlim IConnectionConfigurationValues . BootstrapLock => this . _semaphore ;
62
+
57
63
private TimeSpan _requestTimeout ;
58
64
TimeSpan IConnectionConfigurationValues . RequestTimeout => _requestTimeout ;
59
65
@@ -130,11 +136,10 @@ private static void DefaultApiCallHandler(IApiCallDetails status) { }
130
136
BasicAuthenticationCredentials _basicAuthCredentials ;
131
137
BasicAuthenticationCredentials IConnectionConfigurationValues . BasicAuthenticationCredentials => _basicAuthCredentials ;
132
138
133
- protected IElasticsearchSerializer _serializer ;
139
+ private readonly IElasticsearchSerializer _serializer ;
134
140
IElasticsearchSerializer IConnectionConfigurationValues . Serializer => _serializer ;
135
141
136
142
private readonly IConnectionPool _connectionPool ;
137
- private readonly Func < T , IElasticsearchSerializer > _serializerFactory ;
138
143
IConnectionPool IConnectionConfigurationValues . ConnectionPool => _connectionPool ;
139
144
140
145
private readonly IConnection _connection ;
@@ -147,9 +152,9 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
147
152
{
148
153
this . _connectionPool = connectionPool ;
149
154
this . _connection = connection ?? new HttpConnection ( ) ;
150
- this . _serializerFactory = serializerFactory ?? ( c=> this . DefaultSerializer ( ( T ) this ) ) ;
155
+ serializerFactory = serializerFactory ?? ( c=> this . DefaultSerializer ( ( T ) this ) ) ;
151
156
// ReSharper disable once VirtualMemberCallInContructor
152
- this . _serializer = this . _serializerFactory ( ( T ) this ) ;
157
+ this . _serializer = serializerFactory ( ( T ) this ) ;
153
158
154
159
this . _requestTimeout = ConnectionConfiguration . DefaultTimeout ;
155
160
this . _sniffOnConnectionFault = true ;
@@ -308,6 +313,15 @@ public T BasicAuthentication(string userName, string password)
308
313
/// <para>Note: HTTP pipelining must also be enabled in Elasticsearch for this to work properly.</para>
309
314
/// </summary>
310
315
public T EnableHttpPipelining ( bool enabled = true ) => Assign ( a => a . _enableHttpPipelining = enabled ) ;
316
+
317
+ void IDisposable . Dispose ( ) => this . DisposeManagedResources ( ) ;
318
+
319
+ protected virtual void DisposeManagedResources ( )
320
+ {
321
+ this . _connectionPool ? . Dispose ( ) ;
322
+ this . _connection ? . Dispose ( ) ;
323
+ this . _semaphore ? . Dispose ( ) ;
324
+ }
311
325
}
312
326
}
313
327
0 commit comments