Skip to content

Commit 5bfb556

Browse files
committed
non-connected fix for subscriber
1 parent 7c5bb12 commit 5bfb556

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

src/StackExchange.Redis/MultiGroupDatabase.Async.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public Task<RedisValue> DebugObjectAsync(RedisKey key, CommandFlags flags = Comm
1111
=> GetActiveDatabase().DebugObjectAsync(key, flags);
1212

1313
public Task<EndPoint?> IdentifyEndpointAsync(RedisKey key = default, CommandFlags flags = CommandFlags.None)
14-
=> TryGetActiveDatabase()?.IdentifyEndpointAsync(key, flags) ?? NoEndpoint;
14+
=> TryGetActiveDatabase()?.IdentifyEndpointAsync(key, flags) ?? MultiGroupMultiplexer.NoEndpoint;
1515

1616
public Task KeyMigrateAsync(RedisKey key, EndPoint toServer, int toDatabase = 0, int timeoutMilliseconds = 0, MigrateOptions migrateOptions = MigrateOptions.None, CommandFlags flags = CommandFlags.None)
1717
=> GetActiveDatabase().KeyMigrateAsync(key, toServer, toDatabase, timeoutMilliseconds, migrateOptions, flags);

src/StackExchange.Redis/MultiGroupDatabase.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ internal sealed partial class MultiGroupDatabase(MultiGroupMultiplexer parent, i
1717
// member, for use by members that have an obvious trivial result when disconnected
1818
private IDatabase? TryGetActiveDatabase() => parent.TryGetActive()?.GetDatabase(database, asyncState);
1919

20-
// a completed "no endpoint" result, reused for IdentifyEndpointAsync when the group is fully down
21-
internal static readonly System.Threading.Tasks.Task<System.Net.EndPoint?> NoEndpoint
22-
= System.Threading.Tasks.Task.FromResult<System.Net.EndPoint?>(null);
23-
2420
// Core methods
2521
public IBatch CreateBatch(object? asyncState = null)
2622
=> GetActiveDatabase().CreateBatch(asyncState);

src/StackExchange.Redis/MultiGroupMultiplexer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ static ConnectionMultiplexer Throw() =>
258258
// non-throwing twin of Active, for callers that have a trivial answer when the group is fully down
259259
internal ConnectionMultiplexer? TryGetActive() => _active;
260260

261+
// a completed "no endpoint" result, shared by the database/subscriber facades when the group is fully down
262+
internal static readonly Task<EndPoint?> NoEndpoint = Task.FromResult<EndPoint?>(null);
263+
261264
private ConnectionGroupMember? GetActiveMember()
262265
{
263266
var active = _active;

src/StackExchange.Redis/MultiGroupSubscriber.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ internal sealed partial class MultiGroupSubscriber(MultiGroupMultiplexer parent,
99
// for a lot of things, we can defer through to the active implementation
1010
private ISubscriber GetActiveSubscriber() => parent.Active.GetSubscriber(asyncState);
1111

12+
// non-throwing twin of GetActiveSubscriber: returns null when the group currently has no active
13+
// member, for use by members that have an obvious trivial result when disconnected
14+
private ISubscriber? TryGetActiveSubscriber() => parent.TryGetActive()?.GetSubscriber(asyncState);
15+
1216
public IConnectionMultiplexer Multiplexer => parent;
1317

1418
public bool TryWait(Task task) => GetActiveSubscriber().TryWait(task);
@@ -24,18 +28,18 @@ internal sealed partial class MultiGroupSubscriber(MultiGroupMultiplexer parent,
2428
public Task<TimeSpan> PingAsync(CommandFlags flags = CommandFlags.None) => GetActiveSubscriber().PingAsync(flags);
2529

2630
public EndPoint? IdentifyEndpoint(RedisChannel channel, CommandFlags flags = CommandFlags.None) =>
27-
GetActiveSubscriber().IdentifyEndpoint(channel, flags);
31+
TryGetActiveSubscriber()?.IdentifyEndpoint(channel, flags);
2832

2933
public Task<EndPoint?> IdentifyEndpointAsync(RedisChannel channel, CommandFlags flags = CommandFlags.None) =>
30-
GetActiveSubscriber().IdentifyEndpointAsync(channel, flags);
34+
TryGetActiveSubscriber()?.IdentifyEndpointAsync(channel, flags) ?? MultiGroupMultiplexer.NoEndpoint;
3135

32-
public bool IsConnected(RedisChannel channel = default) => GetActiveSubscriber().IsConnected();
36+
public bool IsConnected(RedisChannel channel = default) => TryGetActiveSubscriber()?.IsConnected() ?? false;
3337

3438
public long Publish(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None)
3539
=> GetActiveSubscriber().Publish(channel, message, flags);
3640

3741
public Task<long> PublishAsync(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None)
3842
=> GetActiveSubscriber().PublishAsync(channel, message, flags);
3943

40-
public EndPoint? SubscribedEndpoint(RedisChannel channel) => GetActiveSubscriber().SubscribedEndpoint(channel);
44+
public EndPoint? SubscribedEndpoint(RedisChannel channel) => TryGetActiveSubscriber()?.SubscribedEndpoint(channel);
4145
}

0 commit comments

Comments
 (0)