Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cieciurm committed Aug 7, 2023
1 parent 5a74cb4 commit 8ee009a
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,35 @@ namespace HealthChecks.DocumentDb;
public class DocumentDbHealthCheck : IHealthCheck
{
private static readonly ConcurrentDictionary<string, DocumentClient> _connections = new();
private readonly string _uriEndpoint;
private readonly string _primaryKey;
private readonly string? _databaseName;
private readonly string? _collectionName;
private readonly DocumentDbOptions _options;

public DocumentDbHealthCheck(DocumentDbOptions documentDbOptions)
{
_uriEndpoint = Guard.ThrowIfNull(documentDbOptions.UriEndpoint);
_primaryKey = Guard.ThrowIfNull(documentDbOptions.PrimaryKey);
_databaseName = documentDbOptions.DatabaseName;
_collectionName = documentDbOptions.CollectionName;
Guard.ThrowIfNull(documentDbOptions.UriEndpoint);
Guard.ThrowIfNull(documentDbOptions.PrimaryKey);

_options = documentDbOptions;
}

/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
if (!_connections.TryGetValue(_uriEndpoint, out var documentDbClient))
if (!_connections.TryGetValue(_options.UriEndpoint, out var documentDbClient))
{
documentDbClient = new DocumentClient(new Uri(_uriEndpoint), _primaryKey);
documentDbClient = new DocumentClient(new Uri(_options.UriEndpoint), _options.PrimaryKey);

if (!_connections.TryAdd(_uriEndpoint, documentDbClient))
if (!_connections.TryAdd(_options.UriEndpoint, documentDbClient))
{
documentDbClient.Dispose();
documentDbClient = _connections[_uriEndpoint];
documentDbClient = _connections[_options.UriEndpoint];
}
}

if (!string.IsNullOrWhiteSpace(_databaseName) && !string.IsNullOrWhiteSpace(_collectionName))
if (!string.IsNullOrWhiteSpace(_options.DatabaseName) && !string.IsNullOrWhiteSpace(_options.CollectionName))
{
await documentDbClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName)).ConfigureAwait(false);
await documentDbClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_options.DatabaseName, _options.CollectionName)).ConfigureAwait(false);
}
else
{
Expand Down

0 comments on commit 8ee009a

Please sign in to comment.