Skip to content

Commit 7c5bb12

Browse files
committed
fix all-down connection scenarios
1 parent 6f4c68d commit 7c5bb12

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/StackExchange.Redis/HealthCheck.StringSetProbe.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public override async Task<HealthCheckResult> CheckHealthAsync(HealthCheck healt
3535
SharedRandom.NextBytes(pooled);
3636
#endif
3737
var payload = (RedisValue)pooled.AsMemory(0, LEN);
38-
Lease<byte>? lease = null;
3938
try
4039
{
4140
// write a value to the db
@@ -52,7 +51,6 @@ await database.LockTakeAsync(
5251
finally
5352
{
5453
ArrayPool<byte>.Shared.Return(pooled);
55-
lease?.Dispose();
5654
}
5755
}
5856
}

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-
=> GetActiveDatabase().IdentifyEndpointAsync(key, flags);
14+
=> TryGetActiveDatabase()?.IdentifyEndpointAsync(key, flags) ?? 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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ internal sealed partial class MultiGroupDatabase(MultiGroupMultiplexer parent, i
1313
// for high DB numbers this might allocate even for null async-state scenarios; unavoidable for now
1414
private IDatabase GetActiveDatabase() => parent.Active.GetDatabase(database, asyncState);
1515

16+
// non-throwing twin of GetActiveDatabase: returns null when the group currently has no active
17+
// member, for use by members that have an obvious trivial result when disconnected
18+
private IDatabase? TryGetActiveDatabase() => parent.TryGetActive()?.GetDatabase(database, asyncState);
19+
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+
1624
// Core methods
1725
public IBatch CreateBatch(object? asyncState = null)
1826
=> GetActiveDatabase().CreateBatch(asyncState);
@@ -27,10 +35,10 @@ public RedisValue DebugObject(RedisKey key, CommandFlags flags = CommandFlags.No
2735
=> GetActiveDatabase().DebugObject(key, flags);
2836

2937
public System.Net.EndPoint? IdentifyEndpoint(RedisKey key = default, CommandFlags flags = CommandFlags.None)
30-
=> GetActiveDatabase().IdentifyEndpoint(key, flags);
38+
=> TryGetActiveDatabase()?.IdentifyEndpoint(key, flags);
3139

3240
public bool IsConnected(RedisKey key, CommandFlags flags = CommandFlags.None)
33-
=> GetActiveDatabase().IsConnected(key, flags);
41+
=> TryGetActiveDatabase()?.IsConnected(key, flags) ?? false;
3442

3543
public System.TimeSpan Ping(CommandFlags flags = CommandFlags.None)
3644
=> GetActiveDatabase().Ping(flags);

src/StackExchange.Redis/MultiGroupMultiplexer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ static ConnectionMultiplexer Throw() =>
255255
}
256256
}
257257

258+
// non-throwing twin of Active, for callers that have a trivial answer when the group is fully down
259+
internal ConnectionMultiplexer? TryGetActive() => _active;
260+
258261
private ConnectionGroupMember? GetActiveMember()
259262
{
260263
var active = _active;

0 commit comments

Comments
 (0)