11using System ;
22using System . Collections . Specialized ;
33using System . ComponentModel ;
4+ using System . Threading ;
45
56namespace Elasticsearch . Net
67{
@@ -10,15 +11,16 @@ namespace Elasticsearch.Net
1011 /// </summary>
1112 public class ConnectionConfiguration : ConnectionConfiguration < ConnectionConfiguration >
1213 {
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 ) ;
1617
1718 /// <summary>
1819 /// ConnectionConfiguration allows you to control how ElasticsearchClient behaves and where/how it connects
1920 /// to elasticsearch
2021 /// </summary>
2122 /// <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" ) ]
2224 public ConnectionConfiguration ( Uri uri = null )
2325 : this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) )
2426 { }
@@ -47,13 +49,17 @@ public ConnectionConfiguration(IConnectionPool connectionPool, Func<ConnectionCo
4749 public ConnectionConfiguration ( IConnectionPool connectionPool , IConnection connection , Func < ConnectionConfiguration , IElasticsearchSerializer > serializerFactory )
4850 : base ( connectionPool , connection , serializerFactory )
4951 { }
52+
5053 }
5154
5255 [ Browsable ( false ) ]
5356 [ EditorBrowsable ( EditorBrowsableState . Never ) ]
5457 public abstract class ConnectionConfiguration < T > : IConnectionConfigurationValues , IHideObjectMembers
5558 where T : ConnectionConfiguration < T >
5659 {
60+ private SemaphoreSlim _semaphore = new SemaphoreSlim ( 1 , 1 ) ;
61+ SemaphoreSlim IConnectionConfigurationValues . BootstrapLock => this . _semaphore ;
62+
5763 private TimeSpan _requestTimeout ;
5864 TimeSpan IConnectionConfigurationValues . RequestTimeout => _requestTimeout ;
5965
@@ -130,11 +136,10 @@ private static void DefaultApiCallHandler(IApiCallDetails status) { }
130136 BasicAuthenticationCredentials _basicAuthCredentials ;
131137 BasicAuthenticationCredentials IConnectionConfigurationValues . BasicAuthenticationCredentials => _basicAuthCredentials ;
132138
133- protected IElasticsearchSerializer _serializer ;
139+ private readonly IElasticsearchSerializer _serializer ;
134140 IElasticsearchSerializer IConnectionConfigurationValues . Serializer => _serializer ;
135141
136142 private readonly IConnectionPool _connectionPool ;
137- private readonly Func < T , IElasticsearchSerializer > _serializerFactory ;
138143 IConnectionPool IConnectionConfigurationValues . ConnectionPool => _connectionPool ;
139144
140145 private readonly IConnection _connection ;
@@ -147,9 +152,9 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
147152 {
148153 this . _connectionPool = connectionPool ;
149154 this . _connection = connection ?? new HttpConnection ( ) ;
150- this . _serializerFactory = serializerFactory ?? ( c=> this . DefaultSerializer ( ( T ) this ) ) ;
155+ serializerFactory = serializerFactory ?? ( c=> this . DefaultSerializer ( ( T ) this ) ) ;
151156 // ReSharper disable once VirtualMemberCallInContructor
152- this . _serializer = this . _serializerFactory ( ( T ) this ) ;
157+ this . _serializer = serializerFactory ( ( T ) this ) ;
153158
154159 this . _requestTimeout = ConnectionConfiguration . DefaultTimeout ;
155160 this . _sniffOnConnectionFault = true ;
@@ -308,6 +313,15 @@ public T BasicAuthentication(string userName, string password)
308313 /// <para>Note: HTTP pipelining must also be enabled in Elasticsearch for this to work properly.</para>
309314 /// </summary>
310315 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+ }
311325 }
312326}
313327
0 commit comments