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

Service Bus + Event Hub: Not a new connection for each check #39

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 9 additions & 10 deletions src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace HealthChecks.AzureServiceBus
public class AzureEventHubHealthCheck
: IHealthCheck
{
private readonly string _connectionString;
private readonly string _eventHubName;
private readonly EventHubClient _eventHubClient;

public AzureEventHubHealthCheck(string connectionString, string eventHubName)
{
if (string.IsNullOrEmpty(connectionString))
Expand All @@ -23,19 +23,18 @@ public AzureEventHubHealthCheck(string connectionString, string eventHubName)
throw new ArgumentNullException(nameof(eventHubName));
}

_connectionString = connectionString;
_eventHubName = eventHubName;
var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString)
{
EntityPath = eventHubName
};
_eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var connectionStringBuilder = new EventHubsConnectionStringBuilder(_connectionString)
{
EntityPath = _eventHubName
};
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
await eventHubClient.GetRuntimeInformationAsync();
await _eventHubClient.GetRuntimeInformationAsync();
return HealthCheckResult.Healthy();
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,21 +14,35 @@ public class AzureServiceBusQueueHealthCheck
private const string TEST_MESSAGE = "HealthCheckTest";
private readonly string _connectionString;
private readonly string _queueName;
private static readonly ConcurrentDictionary<string, ServiceBusConnection> ServiceBusConnections = new ConcurrentDictionary<string, ServiceBusConnection>();

public AzureServiceBusQueueHealthCheck(string connectionString, string queueName)
{
if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException(nameof(connectionString));
if (string.IsNullOrEmpty(queueName)) throw new ArgumentNullException(nameof(queueName));

_connectionString = connectionString;
_queueName = queueName;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var queueClient = new QueueClient(_connectionString,
if (ServiceBusConnections.TryGetValue(_connectionString, out var serviceBusConnection))
{
serviceBusConnection = new ServiceBusConnection(_connectionString);

if (!ServiceBusConnections.TryAdd(_connectionString, serviceBusConnection))
{
return
new HealthCheckResult(context.Registration.FailureStatus, description: "New service bus connection can't be added into dictionary.");
}
}

var queueClient = new QueueClient(serviceBusConnection,
_queueName,
ReceiveMode.PeekLock);
ReceiveMode.PeekLock,
RetryPolicy.NoRetry);

var scheduledMessageId = await queueClient.ScheduleMessageAsync(
new Message(Encoding.UTF8.GetBytes(TEST_MESSAGE)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,6 +14,8 @@ public class AzureServiceBusTopicHealthCheck
private const string TEST_MESSAGE = "HealthCheckTestMessage";
private readonly string _connectionString;
private readonly string _topicName;
private static readonly ConcurrentDictionary<string, ServiceBusConnection> ServiceBusConnections = new ConcurrentDictionary<string, ServiceBusConnection>();

public AzureServiceBusTopicHealthCheck(string connectionString, string topicName)
{
if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException(nameof(connectionString));
Expand All @@ -25,7 +28,18 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
var topicClient = new TopicClient(_connectionString, _topicName);
if (ServiceBusConnections.TryGetValue(_connectionString, out var serviceBusConnection))
{
serviceBusConnection = new ServiceBusConnection(_connectionString);

if (!ServiceBusConnections.TryAdd(_connectionString, serviceBusConnection))
{
return
new HealthCheckResult(context.Registration.FailureStatus, description: "New service bus connection can't be added into dictionary.");
}
}

var topicClient = new TopicClient(serviceBusConnection, _topicName, RetryPolicy.NoRetry);

var scheduledMessageId = await topicClient.ScheduleMessageAsync(
new Message(Encoding.UTF8.GetBytes(TEST_MESSAGE)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.EventHubs" Version="$(MicrosoftAzureEventHubs)" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="$(MicrosoftAzureServiceBus)" />
<PackageReference Include="Microsoft.Azure.EventHubs" Version="2.2.1" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="3.2.1" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="$(MicrosoftExtensionsDiagnosticsHealthChecks)" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public void add_health_check_when_properly_configured()
{
var services = new ServiceCollection();
services.AddHealthChecks()
.AddAzureEventHub("cnn", "hubName");
.AddAzureEventHub("Endpoint=sb://dummynamespace.servicebus.windows.net/;SharedAccessKeyName=DummyAccessKeyName;SharedAccessKey=5dOntTRytoC24opYThisAsit3is2B+OGY1US/fuL3ly=",
"hubName");

var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetService<IOptions<HealthCheckServiceOptions>>();
Expand All @@ -35,8 +36,8 @@ public void add_named_health_check_when_properly_configured()
{
var services = new ServiceCollection();
services.AddHealthChecks()
.AddAzureEventHub("cnn", "hubName",
name: "azureeventhubcheck");
.AddAzureEventHub("Endpoint=sb://dummynamespace.servicebus.windows.net/;SharedAccessKeyName=DummyAccessKeyName;SharedAccessKey=5dOntTRytoC24opYThisAsit3is2B+OGY1US/fuL3ly=",
"hubName", name: "azureeventhubcheck");

var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetService<IOptions<HealthCheckServiceOptions>>();
Expand Down