Skip to content

Commit

Permalink
Azure EventHub - Honour timeout in Health Check
Browse files Browse the repository at this point in the history
  • Loading branch information
cieciurm committed Aug 9, 2023
1 parent 136d8dc commit e9639fc
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false);
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient(context)).ConfigureAwait(false);

_ = await client.GetEventHubPropertiesAsync(cancellationToken).ConfigureAwait(false);

Expand All @@ -59,15 +59,17 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
}
}

private EventHubProducerClient CreateClient()
private EventHubProducerClient CreateClient(HealthCheckContext context)
{
var clientOptions = CreateClientOptions(context);

if (_options.ConnectionString is not null)
return new EventHubProducerClient(GetFullConnectionString());
return new EventHubProducerClient(GetFullConnectionString(), clientOptions);

if (_options.Connection is not null)
return new EventHubProducerClient(_options.Connection);
return new EventHubProducerClient(_options.Connection, clientOptions);

return new EventHubProducerClient(_options.FullyQualifiedNamespace, _options.EventHubName, _options.Credential);
return new EventHubProducerClient(_options.FullyQualifiedNamespace, _options.EventHubName, _options.Credential, clientOptions);
}

private string GetFullConnectionString()
Expand All @@ -78,4 +80,12 @@ private string GetFullConnectionString()
connectionString = $"{connectionString};{ENTITY_PATH_SEGMENT}{_options.EventHubName}";
return connectionString;
}

private static EventHubProducerClientOptions CreateClientOptions(HealthCheckContext context) => new()
{
ConnectionOptions = new()
{
ConnectionIdleTimeout = context.Registration.Timeout,
}
};
}

0 comments on commit e9639fc

Please sign in to comment.