Skip to content

Commit

Permalink
Adjustments to Disposal
Browse files Browse the repository at this point in the history
- Seal the Client classes (no finalizers here)
- Make dispose/async virtual instead of explicit so it will be called in the standard way
- When disposing EventStoreClient cancel the cts
- When disposing EventStoreClient don't create a batchappender if there wasn't one.
  • Loading branch information
timothycoleman committed Jan 28, 2022
1 parent 7699964 commit a58d473
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace EventStore.Client {
/// <summary>
/// The client used to perform maintenance and other administrative tasks on the EventStoreDB.
/// </summary>
public partial class EventStoreOperationsClient : EventStoreClientBase {
public sealed partial class EventStoreOperationsClient : EventStoreClientBase {
private static readonly IDictionary<string, Func<RpcException, Exception>> ExceptionMap =
new Dictionary<string, Func<RpcException, Exception>> {
[Constants.Exceptions.ScavengeNotFound] = ex => new ScavengeNotFoundException(ex.Trailers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace EventStore.Client {
/// <summary>
/// The client used to manage persistent subscriptions in the EventStoreDB.
/// </summary>
public partial class EventStorePersistentSubscriptionsClient : EventStoreClientBase {
public sealed partial class EventStorePersistentSubscriptionsClient : EventStoreClientBase {
private readonly ILogger _log;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace EventStore.Client {
/// <summary>
///The client used to manage projections on the EventStoreDB.
/// </summary>
public partial class EventStoreProjectionManagementClient : EventStoreClientBase {
public sealed partial class EventStoreProjectionManagementClient : EventStoreClientBase {
private readonly ILogger _log;

/// <summary>
Expand Down
31 changes: 15 additions & 16 deletions src/EventStore.Client.Streams/EventStoreClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ namespace EventStore.Client {
/// <summary>
/// The client used for operations on streams.
/// </summary>
public partial class EventStoreClient : EventStoreClientBase,
// todo: remove these long with the explicit implementations
#if !GRPC_CORE
IDisposable,
#endif
IAsyncDisposable {
public sealed partial class EventStoreClient : EventStoreClientBase {

private static readonly JsonSerializerOptions StreamMetadataJsonSerializerOptions = new() {
Converters = {
Expand All @@ -35,9 +30,9 @@ public partial class EventStoreClient : EventStoreClientBase,
#if NET5_0_OR_GREATER
private Lazy<StreamAppender> _streamAppenderLazy;
private StreamAppender _streamAppender => _streamAppenderLazy.Value;
private readonly CancellationTokenSource _disposedTokenSource;
#endif

private readonly CancellationTokenSource _disposedTokenSource;

private static readonly Dictionary<string, Func<RpcException, Exception>> ExceptionMap = new() {
[Constants.Exceptions.InvalidTransaction] =
Expand Down Expand Up @@ -76,8 +71,8 @@ public EventStoreClient(IOptions<EventStoreClientSettings> options) : this(optio
/// <param name="settings"></param>
public EventStoreClient(EventStoreClientSettings? settings = null) : base(settings, ExceptionMap) {
_log = Settings.LoggerFactory?.CreateLogger<EventStoreClient>() ?? new NullLogger<EventStoreClient>();
_disposedTokenSource = new CancellationTokenSource();
#if NET5_0_OR_GREATER
_disposedTokenSource = new CancellationTokenSource();
_streamAppenderLazy = new Lazy<StreamAppender>(CreateStreamAppender);
#endif
}
Expand Down Expand Up @@ -166,22 +161,26 @@ private StreamAppender CreateStreamAppender() {
return options;
}

#if !GRPC_CORE
void IDisposable.Dispose() {
#if NET5_0_OR_GREATER
_streamAppender.Dispose();
#endif
#if !GRPC_CORE && NET5_0_OR_GREATER
/// <inheritdoc />
public override void Dispose() {
_disposedTokenSource.Cancel();
if (_streamAppenderLazy.IsValueCreated)
_streamAppenderLazy.Value.Dispose();
_disposedTokenSource.Dispose();
base.Dispose();
}
#endif

async ValueTask IAsyncDisposable.DisposeAsync() {
#if NET5_0_OR_GREATER
_streamAppender.Dispose();
#endif
/// <inheritdoc />
public override async ValueTask DisposeAsync() {
_disposedTokenSource.Cancel();
_disposedTokenSource.Dispose();
if (_streamAppenderLazy.IsValueCreated)
_streamAppenderLazy.Value.Dispose();
await base.DisposeAsync().ConfigureAwait(false);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace EventStore.Client {
/// <summary>
/// The client used for operations on internal users.
/// </summary>
public class EventStoreUserManagementClient : EventStoreClientBase {
public sealed class EventStoreUserManagementClient : EventStoreClientBase {
private readonly ILogger _log;

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/EventStore.Client/EventStoreClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ private void Rediscover() {

#if !GRPC_CORE
/// <inheritdoc />
public void Dispose() {
public virtual void Dispose() {
_cts.Cancel();
_cts.Dispose();
_channelCache.Dispose();
}
#endif

/// <inheritdoc />
public async ValueTask DisposeAsync() {
public virtual async ValueTask DisposeAsync() {
_cts.Cancel();
_cts.Dispose();
await _channelCache.DisposeAsync().ConfigureAwait(false);
Expand Down

0 comments on commit a58d473

Please sign in to comment.