Skip to content

Commit 4f5ebac

Browse files
kimsey0NickCraver
andauthored
Implement IAsyncDisposable from IConnectionMultiplexer (#2161)
Fixes #2160. We may want to implement `IAsyncDisposable` in more public `IDisposable` classes which can dispose their resources asynchronously. Co-authored-by: Nick Craver <[email protected]>
1 parent 74f4c9a commit 4f5ebac

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fix [#2182](https://github.com/StackExchange/StackExchange.Redis/issues/2182): Be more flexible in which commands are "primary only" in order to support users with replicas that are explicitly configured to allow writes ([#2183 by slorello89](https://github.com/StackExchange/StackExchange.Redis/pull/2183))
6+
- Adds: `IConnectionMultiplexer` now implements `IAsyncDisposable` ([#2161 by kimsey0](https://github.com/StackExchange/StackExchange.Redis/pull/2161))
67

78
## 2.6.48
89

src/StackExchange.Redis/ConnectionMultiplexer.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,21 @@ public void Dispose()
20162016
oldTimer?.Dispose();
20172017
}
20182018

2019+
/// <summary>
2020+
/// Release all resources associated with this object.
2021+
/// </summary>
2022+
public async ValueTask DisposeAsync()
2023+
{
2024+
GC.SuppressFinalize(this);
2025+
await CloseAsync(!_isDisposed);
2026+
if (sentinelConnection is ConnectionMultiplexer sentinel)
2027+
{
2028+
await sentinel.DisposeAsync();
2029+
}
2030+
var oldTimer = Interlocked.Exchange(ref sentinelPrimaryReconnectTimer, null);
2031+
oldTimer?.Dispose();
2032+
}
2033+
20192034
/// <summary>
20202035
/// Close all connections and release all resources associated with this object.
20212036
/// </summary>

src/StackExchange.Redis/Interfaces/IConnectionMultiplexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal interface IInternalConnectionMultiplexer : IConnectionMultiplexer
2020
/// <summary>
2121
/// Represents the abstract multiplexer API.
2222
/// </summary>
23-
public interface IConnectionMultiplexer : IDisposable
23+
public interface IConnectionMultiplexer : IDisposable, IAsyncDisposable
2424
{
2525
/// <summary>
2626
/// Gets the client-name that will be used on all new connections.

src/StackExchange.Redis/PublicAPI.Shipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ StackExchange.Redis.ConnectionMultiplexer.ConfigureAsync(System.IO.TextWriter? l
308308
StackExchange.Redis.ConnectionMultiplexer.ConnectionFailed -> System.EventHandler<StackExchange.Redis.ConnectionFailedEventArgs!>?
309309
StackExchange.Redis.ConnectionMultiplexer.ConnectionRestored -> System.EventHandler<StackExchange.Redis.ConnectionFailedEventArgs!>?
310310
StackExchange.Redis.ConnectionMultiplexer.Dispose() -> void
311+
StackExchange.Redis.ConnectionMultiplexer.DisposeAsync() -> System.Threading.Tasks.ValueTask
311312
StackExchange.Redis.ConnectionMultiplexer.ErrorMessage -> System.EventHandler<StackExchange.Redis.RedisErrorEventArgs!>?
312313
StackExchange.Redis.ConnectionMultiplexer.ExportConfiguration(System.IO.Stream! destination, StackExchange.Redis.ExportOptions options = (StackExchange.Redis.ExportOptions)-1) -> void
313314
StackExchange.Redis.ConnectionMultiplexer.GetCounters() -> StackExchange.Redis.ServerCounters!

tests/StackExchange.Redis.Tests/SharedConnectionFixture.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public event EventHandler<HashSlotMovedEventArgs> HashSlotMoved
128128

129129
public void Dispose() { } // DO NOT call _inner.Dispose();
130130

131+
public ValueTask DisposeAsync() => default; // DO NOT call _inner.DisposeAsync();
132+
131133
public ServerCounters GetCounters() => _inner.GetCounters();
132134

133135
public IDatabase GetDatabase(int db = -1, object? asyncState = null) => _inner.GetDatabase(db, asyncState);

0 commit comments

Comments
 (0)