Skip to content
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

Redis: Renew the underlying connection if there is an exception #1004

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/HealthChecks.Redis/HealthChecks.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.2.88" />
<PackageReference Include="StackExchange.Redis" Version="2.5.43" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.3" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions src/HealthChecks.Redis/RedisHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
}
catch (Exception ex)
{
_connections.TryRemove(_redisConnectionString, out _);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt sbout TryGetValue/TryAdd pair above. See #903, #905 . Also note that ConnectionMultiplexer dictionary value implements IDisposable and should be disposed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the first part, sorry.
Would you like that, instead of using TryGetValue/TryAdd/TryRemove I update it to match the approach from the PR that you have posted?
Related with IDisposable you are right, I'll update my PR with that 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like that, instead of using TryGetValue/TryAdd/TryRemove I update it to match the approach from the PR that you have posted?

Yes. In fact that should be done in many places to get rid of race conditions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification ❤️
I'll update my PR with both changes

Copy link
Contributor Author

@JorTurFer JorTurFer Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm doing the change and I have faced with a problem using GetOrAdd, it hasn't got an async method. That pushes me to use something like this:

var connection = _connections.GetOrAdd(_redisConnectionString, async key =>
{
    return ConnectionMultiplexer.Connect(key);
}); 

My concern here is about doing sync calls inside the async context of the health check execution. Maybe I create a bigger problem with that than the original race condition with TryAdd/TryGet/TryRemove.
Any thoughts/suggestion @sungam3r ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Let's keep as is for now.

return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
Expand Down