-
Notifications
You must be signed in to change notification settings - Fork 804
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
base: master
Are you sure you want to change the base?
Introduce async factories to Redis health check. #2305
Conversation
/// <returns>The specified <paramref name="builder"/>.</returns> | ||
public static IHealthChecksBuilder AddRedis( | ||
this IHealthChecksBuilder builder, | ||
Func<IServiceProvider, CancellationToken, Task<string?>> connectionStringFactory, |
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).
Co-authored-by: Eric Erhardt <[email protected]>
Co-authored-by: Eric Erhardt <[email protected]>
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Is this caching now broken if I create multiple RedisHealthCheck
instances with the same connection string value? They each will get a different delegate instance now, where before it was keyed off the actual string value.
What this PR does / why we need it:
This PR introduces variants of the
AddRedis(...)
method which takes async factories for connection string and connection multiplexer.On the .NET Aspire team we are using these libraries to implement health check support for orchestrated resources. We use health checks in two scenarios. The first is inside an orchestrated service (e.g. a .NET Web API project) and the second is in the app host that does the orchestrating.
It is this second scenario that motivates this proposed change. Here is our current usage of this API in the
Azure.Hosting.Redis
package:https://github.com/dotnet/aspire/blob/4e7e0de000bb83f824cca2773892a22b5da89245/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L36-L62
Notice that we make use of the existing non-async factory method for
AddRedis(...)
. However for this to work we need to declare a variable in the outer scope calledconnectionString
and then capture that variable in both an event handling callback to get the connection string which is only available AFTER the DI container has been created and is only available via an async call. We do it this way to avoid doing sync over async.With the change in this PR we might be able to greatly simplify this code, down to the following:
Which issue(s) this PR fixes:
Please reference the issue this PR will close: #[issue number]
Special notes for your reviewer:
Does this PR introduce a user-facing change?:
Yes.
Please make sure you've completed the relevant tasks for this PR, out of the following list: