-
Notifications
You must be signed in to change notification settings - Fork 801
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
[RabbitMQ] Reusing healthy connection #352
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
14495bd
Reusing same RabbitMQHealthCheck instance
rkarg-blizz c60e42b
Explicit handling of null sslOption parameter
rkarg-blizz bee79fc
Reusing existing connection if it's still open
rkarg-blizz 4f81309
Using valid Uri string for tests
rkarg-blizz ec3dcdc
Merge branch 'master' into master
rwkarg b3d7553
Reconnect only used if a ConnectionFactory is available
rkarg-blizz cf8e8aa
Adding initial unit tests of the RabbitMQHealthCheck itself
rkarg-blizz 3584d00
[style] Using discard and throw expression pattern
rkarg-blizz 3b47a85
[style] removing blank lines
rkarg-blizz 388d7ac
removing redundant tests
rkarg-blizz 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 |
---|---|---|
@@ -1,72 +1,84 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using RabbitMQ.Client; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HealthChecks.RabbitMQ | ||
{ | ||
public class RabbitMQHealthCheck | ||
: IHealthCheck | ||
{ | ||
private readonly Lazy<IConnectionFactory> _lazyConnectionFactory; | ||
private readonly IConnection _rmqConnection; | ||
|
||
public RabbitMQHealthCheck(string rabbitMqConnectionString, SslOption sslOption = null) | ||
{ | ||
if (rabbitMqConnectionString == null) throw new ArgumentNullException(nameof(rabbitMqConnectionString)); | ||
|
||
_lazyConnectionFactory = new Lazy<IConnectionFactory>(() => new ConnectionFactory() | ||
{ | ||
Uri = new Uri(rabbitMqConnectionString), | ||
Ssl = sslOption ?? new SslOption() | ||
}); | ||
} | ||
|
||
public RabbitMQHealthCheck(IConnection connection) | ||
{ | ||
_rmqConnection = connection ?? throw new ArgumentNullException(nameof(connection)); | ||
} | ||
|
||
public RabbitMQHealthCheck(IConnectionFactory connectionFactory) | ||
{ | ||
_lazyConnectionFactory = new Lazy<IConnectionFactory>(() => | ||
connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory))); | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
if (_rmqConnection != null) | ||
{ | ||
return TestConnection(_rmqConnection); | ||
} | ||
|
||
using (var connection = CreateConnection(_lazyConnectionFactory.Value)) | ||
{ | ||
return TestConnection(connection); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return Task.FromResult( | ||
new HealthCheckResult(context.Registration.FailureStatus, exception: ex)); | ||
} | ||
} | ||
|
||
private static Task<HealthCheckResult> TestConnection(IConnection connection) | ||
{ | ||
using (var channel = connection.CreateModel()) | ||
{ | ||
return Task.FromResult( | ||
HealthCheckResult.Healthy()); | ||
} | ||
} | ||
|
||
private static IConnection CreateConnection(IConnectionFactory connectionFactory) | ||
{ | ||
return connectionFactory.CreateConnection("Health Check Connection"); | ||
} | ||
} | ||
} | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using RabbitMQ.Client; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HealthChecks.RabbitMQ | ||
{ | ||
public class RabbitMQHealthCheck | ||
: IHealthCheck | ||
{ | ||
private readonly IConnectionFactory _connectionFactory; | ||
private IConnection _rmqConnection; | ||
|
||
public RabbitMQHealthCheck(string rabbitMqConnectionString, SslOption sslOption = null) | ||
{ | ||
var connectionFactory = new ConnectionFactory | ||
{ | ||
Uri = new Uri(rabbitMqConnectionString ?? throw new ArgumentNullException(nameof(rabbitMqConnectionString))), | ||
AutomaticRecoveryEnabled = true // Explicitly setting to ensure this is true (in case the default changes) | ||
}; | ||
if (sslOption != null) | ||
{ | ||
connectionFactory.Ssl = sslOption; | ||
} | ||
_connectionFactory = connectionFactory; | ||
} | ||
|
||
public RabbitMQHealthCheck(IConnection connection) | ||
{ | ||
_rmqConnection = connection ?? throw new ArgumentNullException(nameof(connection)); | ||
} | ||
|
||
public RabbitMQHealthCheck(IConnectionFactory connectionFactory) | ||
{ | ||
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
// If no factory was provided then we're stuck using the passed in connection | ||
// regardless of the state it may be in. We don't have a way to attempt to | ||
// create a new connection :( | ||
if (_connectionFactory == null) | ||
{ | ||
return TestConnection(_rmqConnection); | ||
} | ||
|
||
if (_rmqConnection != null && _rmqConnection.IsOpen == false) | ||
{ | ||
_rmqConnection.Close(0); | ||
_rmqConnection = null; | ||
} | ||
if (_rmqConnection == null) | ||
{ | ||
_rmqConnection = CreateConnection(_connectionFactory); | ||
} | ||
|
||
return TestConnection(_rmqConnection); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return Task.FromResult( | ||
new HealthCheckResult(context.Registration.FailureStatus, exception: ex)); | ||
} | ||
} | ||
|
||
private static Task<HealthCheckResult> TestConnection(IConnection connection) | ||
{ | ||
using (connection.CreateModel()) | ||
{ | ||
return Task.FromResult( | ||
HealthCheckResult.Healthy()); | ||
} | ||
} | ||
|
||
private static IConnection CreateConnection(IConnectionFactory connectionFactory) | ||
{ | ||
return connectionFactory.CreateConnection("Health Check Connection"); | ||
} | ||
} | ||
} |
20 changes: 15 additions & 5 deletions
20
test/UnitTests/DependencyInjection/RabbitMQ/RabbitMQUnitTests.cs
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.
Why?
I
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.
When passing in a Func to HealthCheckRegistration, that Func gets called every time that a health check is evaluated.
This resulted in a new RabbitMQHealthCheck instance being created for every health check evaluation. This further lead to a new ConnectionFactory and Connection being created for each health check evaluation which eliminated reuse (a RabbitMQHealthCheck will reuse an already provided/created ConnectionFactory/Connection but if a new RabbitMQHealthCheck is created each time then that reuse can't happen since it's a fresh instance each time).
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.
Hi @rwkarg
True, I try to merge asap!