Skip to content

Commit

Permalink
Construct RabbitMQHealthCheck only from options
Browse files Browse the repository at this point in the history
  • Loading branch information
DPDmancul committed Feb 2, 2023
1 parent 5250a98 commit bd391d0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static IHealthChecksBuilder AddRabbitMQ(
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
_ => new RabbitMQHealthCheck(rabbitConnectionString, sslOption),
_ => new RabbitMQHealthCheck(new RabbitMQHealthCheckOptions { ConnectionUri = rabbitConnectionString, Ssl = sslOption }),
failureStatus,
tags,
timeout));
Expand Down Expand Up @@ -99,11 +99,11 @@ public static IHealthChecksBuilder AddRabbitMQ(
if (connection != null)
{
return new RabbitMQHealthCheck(connection);
return new RabbitMQHealthCheck(new RabbitMQHealthCheckOptions { Connection = connection });
}
else if (connectionFactory != null)
{
return new RabbitMQHealthCheck(connectionFactory);
return new RabbitMQHealthCheck(new RabbitMQHealthCheckOptions { ConnectionFactory = connectionFactory });
}
else
{
Expand Down Expand Up @@ -148,7 +148,7 @@ public static IHealthChecksBuilder AddRabbitMQ(
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => new RabbitMQHealthCheck(connectionFactory(sp)),
sp => new RabbitMQHealthCheck(new RabbitMQHealthCheckOptions { Connection = connectionFactory(sp) }),
failureStatus,
tags,
timeout));
Expand Down Expand Up @@ -181,7 +181,7 @@ public static IHealthChecksBuilder AddRabbitMQ(
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => new RabbitMQHealthCheck(connectionFactoryFactory(sp)),
sp => new RabbitMQHealthCheck(new RabbitMQHealthCheckOptions { ConnectionFactory = connectionFactoryFactory(sp) }),
failureStatus,
tags,
timeout));
Expand Down Expand Up @@ -216,7 +216,7 @@ public static IHealthChecksBuilder AddRabbitMQ(
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => new RabbitMQHealthCheck(connectionStringFactory(sp), sslOption),
sp => new RabbitMQHealthCheck(new RabbitMQHealthCheckOptions { ConnectionUri = connectionStringFactory(sp), Ssl = sslOption }),
failureStatus,
tags,
timeout));
Expand Down
67 changes: 40 additions & 27 deletions src/HealthChecks.Rabbitmq/RabbitMQHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,31 @@ public class RabbitMQHealthCheck : IHealthCheck
private static readonly ConcurrentDictionary<RabbitMQHealthCheckOptions, IConnection> _connections = new();

private IConnection? _connection;
private readonly IConnectionFactory? _factory;
private readonly RabbitMQHealthCheckOptions _options;

public RabbitMQHealthCheck(IConnection connection)
public RabbitMQHealthCheck(RabbitMQHealthCheckOptions options)
{
_connection = Guard.ThrowIfNull(connection);
_options = new RabbitMQHealthCheckOptions(new Uri(connection.Endpoint.ToString()));
}

public RabbitMQHealthCheck(IConnectionFactory factory)
{
_factory = Guard.ThrowIfNull(factory);
_options = new RabbitMQHealthCheckOptions(factory.Uri);
}

public RabbitMQHealthCheck(Uri rabbitConnectionString, SslOption? ssl)
{
_factory = new ConnectionFactory
{
Uri = rabbitConnectionString,
AutomaticRecoveryEnabled = true,
UseBackgroundThreadsForIO = true,
};
_options = new RabbitMQHealthCheckOptions(rabbitConnectionString);
_options = options;
_connection = options.Connection;

if (ssl != null)
if (_connection is null && _options.ConnectionFactory is null && _options.ConnectionUri is null)
{
((ConnectionFactory)_factory).Ssl = ssl;
_options.Ssl = ssl;
throw new ArgumentException("A connection, connnection factory, or connection string must be set!", nameof(options));
}
}

[Obsolete("Please provide RabbitMQHealthCheckOptions")]

This comment has been minimized.

Copy link
@sungam3r

sungam3r Feb 3, 2023

Collaborator

You may remove all obsolete code since master now tracks breaking changes for v7 release. See #1593

public RabbitMQHealthCheck(IConnection connection) : this(new RabbitMQHealthCheckOptions { Connection = connection })
{ }

[Obsolete("Please provide RabbitMQHealthCheckOptions")]
public RabbitMQHealthCheck(IConnectionFactory factory) : this(new RabbitMQHealthCheckOptions { ConnectionFactory = factory })
{ }

[Obsolete("Please provide RabbitMQHealthCheckOptions")]
public RabbitMQHealthCheck(Uri rabbitConnectionString, SslOption? ssl) : this(new RabbitMQHealthCheckOptions { ConnectionUri = rabbitConnectionString, Ssl = ssl })
{ }

/// <inheritdoc />
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
Expand All @@ -60,10 +53,30 @@ public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, Canc

private IConnection EnsureConnection()
{
if (_connection == null)
if (_connection is null)
{
Guard.ThrowIfNull(_factory);
_connection = _connections.GetOrAdd(_options, _ => _factory.CreateConnection());
_connection = _connections.GetOrAdd(_options, _ =>
{
var factory = _options.ConnectionFactory;
if (factory is null)
{
Guard.ThrowIfNull(_options.ConnectionUri);
factory = new ConnectionFactory
{
Uri = _options.ConnectionUri,
AutomaticRecoveryEnabled = true,
UseBackgroundThreadsForIO = true,
};
if (_options.Ssl is not null)
{
((ConnectionFactory)factory).Ssl = _options.Ssl;
}
}
return factory.CreateConnection();
});
}

return _connection;
Expand Down
30 changes: 24 additions & 6 deletions src/HealthChecks.Rabbitmq/RabbitMQHealthCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@ namespace HealthChecks.RabbitMQ;
/// </summary>
public class RabbitMQHealthCheckOptions
{
public Uri Uri { get; set; }
public SslOption? Ssl { get; set; }
/// <summary>
/// An already created connection to RabbitMQ.
/// </summary>
public IConnection? Connection { get; set; }

/// <summary>
/// A connection factory for RabbitMQ.
/// </summary>
public IConnectionFactory? ConnectionFactory { get; set; }

public RabbitMQHealthCheckOptions(Uri uri)
{
Uri = uri;
}
/// <summary>
/// An Uri representing a connection string for RabbitMQ.
/// </summary>
/// <remarks>
/// Can be used in conjunction with the <see cref="SslOption"/> property.
/// </remarks>
public Uri? ConnectionUri { get; set; }

/// <summary>
/// The SSL options for a connection string.
/// </summary>
/// <remarks>
/// Must be used in conjunction with the <see cref="ConnectionUri"/> property.
/// </remarks>
public SslOption? Ssl { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ namespace HealthChecks.RabbitMQ
{
public class RabbitMQHealthCheck : Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
{
public RabbitMQHealthCheck(HealthChecks.RabbitMQ.RabbitMQHealthCheckOptions options) { }
[System.Obsolete("Please provide RabbitMQHealthCheckOptions")]
public RabbitMQHealthCheck(RabbitMQ.Client.IConnection connection) { }
[System.Obsolete("Please provide RabbitMQHealthCheckOptions")]
public RabbitMQHealthCheck(RabbitMQ.Client.IConnectionFactory factory) { }
[System.Obsolete("Please provide RabbitMQHealthCheckOptions")]
public RabbitMQHealthCheck(System.Uri rabbitConnectionString, RabbitMQ.Client.SslOption? ssl) { }
public System.Threading.Tasks.Task<Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult> CheckHealthAsync(Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckContext context, System.Threading.CancellationToken cancellationToken = default) { }
}
public class RabbitMQHealthCheckOptions
{
public RabbitMQOptions(System.Uri uri) { }
public RabbitMQHealthCheckOptions() { }
public RabbitMQ.Client.IConnection? Connection { get; set; }
public RabbitMQ.Client.IConnectionFactory? ConnectionFactory { get; set; }
public System.Uri? ConnectionUri { get; set; }
public RabbitMQ.Client.SslOption? Ssl { get; set; }
public System.Uri Uri { get; set; }
}
}
namespace Microsoft.Extensions.DependencyInjection
Expand Down

0 comments on commit bd391d0

Please sign in to comment.