-
Notifications
You must be signed in to change notification settings - Fork 812
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
Introduce async factories to Redis health check. #2305
Open
mitchdenny
wants to merge
3
commits into
Xabaril:master
Choose a base branch
from
mitchdenny:mitchdenny/redis-async-factories
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,19 @@ namespace HealthChecks.Redis; | |
/// </summary> | ||
public class RedisHealthCheck : IHealthCheck | ||
{ | ||
private static readonly ConcurrentDictionary<string, IConnectionMultiplexer> _connections = new(); | ||
private readonly string? _redisConnectionString; | ||
private static readonly ConcurrentDictionary<Func<CancellationToken, Task<string?>>, IConnectionMultiplexer> _connections = new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this caching now broken if I create multiple |
||
private readonly Func<CancellationToken, Task<string?>>? _redisConnectionStringFactory; | ||
private readonly IConnectionMultiplexer? _connectionMultiplexer; | ||
private readonly Func<IConnectionMultiplexer>? _connectionMultiplexerFactory; | ||
private readonly Func<CancellationToken, Task<IConnectionMultiplexer>>? _connectionMultiplexerFactory; | ||
|
||
public RedisHealthCheck(string redisConnectionString) | ||
{ | ||
_redisConnectionString = Guard.ThrowIfNull(redisConnectionString); | ||
_redisConnectionStringFactory = (_) => Task.FromResult<string?>(Guard.ThrowIfNull(redisConnectionString)); | ||
} | ||
|
||
public RedisHealthCheck(Func<CancellationToken, Task<string?>> redisConnectionStringFactory) | ||
{ | ||
_redisConnectionStringFactory = Guard.ThrowIfNull(redisConnectionStringFactory); | ||
} | ||
|
||
public RedisHealthCheck(IConnectionMultiplexer connectionMultiplexer) | ||
|
@@ -35,6 +40,11 @@ public RedisHealthCheck(IConnectionMultiplexer connectionMultiplexer) | |
/// <seealso cref="IConnectionMultiplexer"/> for the first time, so exceptions can be handled gracefully. | ||
/// </remarks> | ||
internal RedisHealthCheck(Func<IConnectionMultiplexer> connectionMultiplexerFactory) | ||
{ | ||
_connectionMultiplexerFactory = (ct) => Task.FromResult(connectionMultiplexerFactory()); | ||
} | ||
|
||
internal RedisHealthCheck(Func<CancellationToken, Task<IConnectionMultiplexer>> connectionMultiplexerFactory) | ||
{ | ||
_connectionMultiplexerFactory = connectionMultiplexerFactory; | ||
} | ||
|
@@ -44,25 +54,31 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context | |
{ | ||
try | ||
{ | ||
IConnectionMultiplexer? connection = _connectionMultiplexer ?? _connectionMultiplexerFactory?.Invoke(); | ||
var connection = (_connectionMultiplexer, _connectionMultiplexerFactory) switch | ||
{ | ||
(not null, _) => _connectionMultiplexer, | ||
(null, { } factory) => await factory(cancellationToken).ConfigureAwait(false), | ||
_ => null | ||
}; | ||
|
||
if (_redisConnectionString is not null && !_connections.TryGetValue(_redisConnectionString, out connection)) | ||
if (_redisConnectionStringFactory is not null && !_connections.TryGetValue(_redisConnectionStringFactory, out connection)) | ||
{ | ||
try | ||
{ | ||
var connectionMultiplexerTask = ConnectionMultiplexer.ConnectAsync(_redisConnectionString!); | ||
var redisConnectionString = await _redisConnectionStringFactory(cancellationToken).ConfigureAwait(false); | ||
var connectionMultiplexerTask = ConnectionMultiplexer.ConnectAsync(redisConnectionString!); | ||
connection = await TimeoutAsync(connectionMultiplexerTask, cancellationToken).ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, description: "Healthcheck timed out"); | ||
} | ||
|
||
if (!_connections.TryAdd(_redisConnectionString, connection)) | ||
if (!_connections.TryAdd(_redisConnectionStringFactory, connection)) | ||
{ | ||
// Dispose new connection which we just created, because we don't need it. | ||
connection.Dispose(); | ||
connection = _connections[_redisConnectionString]; | ||
connection = _connections[_redisConnectionStringFactory]; | ||
} | ||
} | ||
|
||
|
@@ -99,9 +115,9 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context | |
} | ||
catch (Exception ex) | ||
{ | ||
if (_redisConnectionString is not null) | ||
if (_redisConnectionStringFactory is not null) | ||
{ | ||
_connections.TryRemove(_redisConnectionString, out var connection); | ||
_connections.TryRemove(_redisConnectionStringFactory, out var connection); | ||
#pragma warning disable IDISP007 // Don't dispose injected [false positive here] | ||
connection?.Dispose(); | ||
#pragma warning restore IDISP007 // Don't dispose injected | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overload feels weird to me. It is too specific to the Aspire use case.
If you need to do something async, I think you should create the IConnectionMultiplexer asynchronously. Getting the connection string asynchronously isn't very common.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if delayed acquisition of connection strings is Aspire specific. It certainly impacts Aspire, but there are plenty of times that the connection string is not immediately available (particularly in multi-tenant scenarios).