Skip to content

Spike/dispose #1720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Elasticsearch.Net/Auditing/Audit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public class Audit
public string Path { get; internal set; }
public Exception Exception { get; internal set; }

public Audit(AuditEvent type, DateTime occured)
public Audit(AuditEvent type, DateTime started)
{
this.Event = type;
this.Started = occured;
this.Started = started;
}

public override string ToString()
{
var took = Started - Ended;
return $"Node: {Node?.Uri}, Event: {Event.GetStringValue()} NodeAlive: {Node?.IsAlive}, Took: {took.ToString()}";
return $"Node: {Node?.Uri}, Event: {Event.GetStringValue()} NodeAlive: {Node?.IsAlive}, Took: {took}";
}
}
}
28 changes: 21 additions & 7 deletions src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;

namespace Elasticsearch.Net
{
Expand All @@ -10,15 +11,16 @@ namespace Elasticsearch.Net
/// </summary>
public class ConnectionConfiguration : ConnectionConfiguration<ConnectionConfiguration>
{
public static TimeSpan DefaultTimeout = TimeSpan.FromMinutes(1);
public static TimeSpan DefaultPingTimeout = TimeSpan.FromSeconds(2);
public static TimeSpan DefaultPingTimeoutOnSSL = TimeSpan.FromSeconds(5);
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromMinutes(1);
public static readonly TimeSpan DefaultPingTimeout = TimeSpan.FromSeconds(2);
public static readonly TimeSpan DefaultPingTimeoutOnSSL = TimeSpan.FromSeconds(5);

/// <summary>
/// ConnectionConfiguration allows you to control how ElasticsearchClient behaves and where/how it connects
/// to elasticsearch
/// </summary>
/// <param name="uri">The root of the elasticsearch node we want to connect to. Defaults to http://localhost:9200</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public ConnectionConfiguration(Uri uri = null)
: this(new SingleNodeConnectionPool(uri ?? new Uri("http://localhost:9200")))
{ }
Expand Down Expand Up @@ -47,13 +49,17 @@ public ConnectionConfiguration(IConnectionPool connectionPool, Func<ConnectionCo
public ConnectionConfiguration(IConnectionPool connectionPool, IConnection connection, Func<ConnectionConfiguration, IElasticsearchSerializer> serializerFactory)
: base(connectionPool, connection, serializerFactory)
{ }

}

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class ConnectionConfiguration<T> : IConnectionConfigurationValues, IHideObjectMembers
where T : ConnectionConfiguration<T>
{
private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
SemaphoreSlim IConnectionConfigurationValues.BootstrapLock => this._semaphore;

private TimeSpan _requestTimeout;
TimeSpan IConnectionConfigurationValues.RequestTimeout => _requestTimeout;

Expand Down Expand Up @@ -130,11 +136,10 @@ private static void DefaultApiCallHandler(IApiCallDetails status) { }
BasicAuthenticationCredentials _basicAuthCredentials;
BasicAuthenticationCredentials IConnectionConfigurationValues.BasicAuthenticationCredentials => _basicAuthCredentials;

protected IElasticsearchSerializer _serializer;
private readonly IElasticsearchSerializer _serializer;
IElasticsearchSerializer IConnectionConfigurationValues.Serializer => _serializer;

private readonly IConnectionPool _connectionPool;
private readonly Func<T, IElasticsearchSerializer> _serializerFactory;
IConnectionPool IConnectionConfigurationValues.ConnectionPool => _connectionPool;

private readonly IConnection _connection;
Expand All @@ -147,9 +152,9 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
{
this._connectionPool = connectionPool;
this._connection = connection ?? new HttpConnection();
this._serializerFactory = serializerFactory ?? (c=>this.DefaultSerializer((T)this));
serializerFactory = serializerFactory ?? (c=>this.DefaultSerializer((T)this));
// ReSharper disable once VirtualMemberCallInContructor
this._serializer = this._serializerFactory((T)this);
this._serializer = serializerFactory((T)this);

this._requestTimeout = ConnectionConfiguration.DefaultTimeout;
this._sniffOnConnectionFault = true;
Expand Down Expand Up @@ -308,6 +313,15 @@ public T BasicAuthentication(string userName, string password)
/// <para>Note: HTTP pipelining must also be enabled in Elasticsearch for this to work properly.</para>
/// </summary>
public T EnableHttpPipelining(bool enabled = true) => Assign(a => a._enableHttpPipelining = enabled);

void IDisposable.Dispose() => this.DisposeManagedResources();

protected virtual void DisposeManagedResources()
{
this._connectionPool?.Dispose();
this._connection?.Dispose();
this._semaphore?.Dispose();
}
}
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Specialized;
using System.Threading;

namespace Elasticsearch.Net
{
public interface IConnectionConfigurationValues
public interface IConnectionConfigurationValues : IDisposable
{
/// <summary> Provides a semaphoreslim to transport implementations that need to limit access to a resource</summary>
SemaphoreSlim BootstrapLock { get; }

/// <summary> The connection pool to use when talking with elasticsearch </summary>
IConnectionPool ConnectionPool { get; }

Expand Down
4 changes: 4 additions & 0 deletions src/Elasticsearch.Net/Connection/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,9 @@ private void HandleException<TReturn>(ResponseBuilder<TReturn> builder, WebExcep
builder.Stream = response.GetResponseStream();
}
}

void IDisposable.Dispose() => this.DisposeManagedResources();

protected virtual void DisposeManagedResources() { }
}
}
5 changes: 3 additions & 2 deletions src/Elasticsearch.Net/Connection/IConnection.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;

namespace Elasticsearch.Net
{
public interface IConnection
public interface IConnection : IDisposable
{
Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(RequestData requestData)
where TReturn : class;
Expand Down
2 changes: 1 addition & 1 deletion src/Elasticsearch.Net/ConnectionPool/IConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Elasticsearch.Net
{
public interface IConnectionPool
public interface IConnectionPool : IDisposable
{
/// <summary>
/// Returns a readonly constant view of all the nodes in the cluster, this might involve creating copies of the nodes e.g
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ public SingleNodeConnectionPool(Uri uri, IDateTimeProvider dateTimeProvider = nu
}

public IEnumerable<Node> CreateView(Action<AuditEvent, Node> audit = null) => this.Nodes;

void IDisposable.Dispose() => this.DisposeManagedResources();

protected virtual void DisposeManagedResources() { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,10 @@ public override IEnumerable<Node> CreateView(Action<AuditEvent, Node> audit = nu
}
}

protected override void DisposeManagedResources()
{
this._readerWriter?.Dispose();
base.DisposeManagedResources();
}
}
}
3 changes: 3 additions & 0 deletions src/Elasticsearch.Net/ConnectionPool/StaticConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@ public virtual IEnumerable<Node> CreateView(Action<AuditEvent, Node> audit = nul
}
}

void IDisposable.Dispose() => this.DisposeManagedResources();

protected virtual void DisposeManagedResources() { }
}
}
2 changes: 2 additions & 0 deletions src/Elasticsearch.Net/Elasticsearch.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<Prefer32Bit>false</Prefer32Bit>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -54,6 +55,7 @@
<ItemGroup>
<Compile Include="Configuration\RequestConfiguration.cs" />
<Compile Include="Exceptions\UnexpectedElasticsearchClientException.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Transport\Pipeline\PipelineException.cs" />
<Compile Include="Connection\HttpMethod.cs" />
<Compile Include="Auditing\Audit.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/Elasticsearch.Net/ElasticsearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ public partial class ElasticsearchClient : IElasticsearchClient
protected ITransport<IConnectionConfigurationValues> Transport { get; set; }

/// <summary>Instantiate a new low level elasticsearch client to http://localhost:9200</summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public ElasticsearchClient() : this(new Transport<IConnectionConfigurationValues>(new ConnectionConfiguration())) { }

/// <summary>Instantiate a new low level elasticsearch client using the specified settings</summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public ElasticsearchClient(IConnectionConfigurationValues settings) : this(new Transport<IConnectionConfigurationValues>(settings ?? new ConnectionConfiguration())) { }

/// <summary>
Expand Down
Binary file added src/Elasticsearch.Net/GlobalSuppressions.cs
Binary file not shown.
4 changes: 3 additions & 1 deletion src/Elasticsearch.Net/Transport/ITransport.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Threading.Tasks;

namespace Elasticsearch.Net
{
public interface ITransport<out TConnectionSettings> where TConnectionSettings : IConnectionConfigurationValues
public interface ITransport<out TConnectionSettings>
where TConnectionSettings : IConnectionConfigurationValues
{
TConnectionSettings Settings { get; }

Expand Down
Loading