Skip to content

Commit

Permalink
Introduce RabbitMQHealthCheckOptions.RequestedConnectionTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jul 22, 2023
1 parent 46f3ded commit 029dc95
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/HealthChecks.Rabbitmq/RabbitMQHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public RabbitMQHealthCheck(RabbitMQHealthCheckOptions options)
/// <inheritdoc />
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
// TODO: cancellationToken unused, see https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/714
try
{
using var model = EnsureConnection().CreateModel();
Expand All @@ -52,9 +53,14 @@ private IConnection EnsureConnection()
{
Uri = _options.ConnectionUri,
AutomaticRecoveryEnabled = true,
UseBackgroundThreadsForIO = true,
UseBackgroundThreadsForIO = true
};
if (_options.RequestedConnectionTimeout is not null)
{
((ConnectionFactory)factory).RequestedConnectionTimeout = _options.RequestedConnectionTimeout.Value;
}
if (_options.Ssl is not null)
{
((ConnectionFactory)factory).Ssl = _options.Ssl;
Expand Down
5 changes: 5 additions & 0 deletions src/HealthChecks.Rabbitmq/RabbitMQHealthCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public class RabbitMQHealthCheckOptions
/// Must be used in conjunction with the <see cref="ConnectionUri"/> property.
/// </remarks>
public SslOption? Ssl { get; set; }

/// <summary>
/// Timeout setting for connection attempts.
/// </summary>
public TimeSpan? RequestedConnectionTimeout { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net;
using Microsoft.Extensions.DependencyInjection;

Check failure on line 2 in test/HealthChecks.RabbitMQ.Tests/Functional/RabbitHealthCheckTests.cs

View workflow job for this annotation

GitHub Actions / build

Using directive is unnecessary.

Check failure on line 2 in test/HealthChecks.RabbitMQ.Tests/Functional/RabbitHealthCheckTests.cs

View workflow job for this annotation

GitHub Actions / build

Using directive is unnecessary.
using Microsoft.Extensions.Diagnostics.HealthChecks;
using RabbitMQ.Client;

namespace HealthChecks.RabbitMQ.Tests.Functional;
Expand Down Expand Up @@ -272,4 +274,30 @@ public async Task two_rabbitmq_health_check()
response1.StatusCode.ShouldBe(HttpStatusCode.OK);
response2.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}

// https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/714
[Fact]
public async Task should_respect_timeout()
{
var services = new ServiceCollection();

services
.AddLogging()
.AddHealthChecks()
.AddRabbitMQ(opt =>
{
opt.RequestedConnectionTimeout = TimeSpan.FromSeconds(1);
opt.ConnectionUri = new Uri($"amqps://user:pwd@invalid-host:5672");
},
timeout: TimeSpan.FromSeconds(10));

using var provider = services.BuildServiceProvider();
var healthCheckService = provider.GetRequiredService<HealthCheckService>();
var start = DateTime.Now;
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var report = await healthCheckService.CheckHealthAsync(cts.Token).ConfigureAwait(false);
report.Status.ShouldBe(HealthStatus.Unhealthy);
var end = DateTime.Now;
(end - start).ShouldBeLessThan(TimeSpan.FromSeconds(10));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace HealthChecks.RabbitMQ
public RabbitMQ.Client.IConnection? Connection { get; set; }
public RabbitMQ.Client.IConnectionFactory? ConnectionFactory { get; set; }
public System.Uri? ConnectionUri { get; set; }
public System.TimeSpan? RequestedConnectionTimeout { get; set; }
public RabbitMQ.Client.SslOption? Ssl { get; set; }
}
}
Expand Down

0 comments on commit 029dc95

Please sign in to comment.