-
Notifications
You must be signed in to change notification settings - Fork 849
Use recommended NATS.Net client for Nats package #2336
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
40fd47b
Use recommnded NATS.Net client for nats package
Alirexaa 6dad3ad
Remove Nats.Client from packages props
Alirexaa 7854598
Try resolve NatsConnection before INatsConnection
Alirexaa dda11db
Merge branch 'master' of https://github.com/Xabaril/AspNetCore.Diagno…
Alirexaa 133fd97
Address PR feedback
Alirexaa 57221a8
Update readme
Alirexaa c5b7238
Addresss PR feedback
Alirexaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,111 +1,38 @@ | ||
| using System.Text; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using NATS.Client; | ||
| using NATS.Client.Core; | ||
|
|
||
| namespace HealthChecks.Nats; | ||
|
|
||
| /// <summary> | ||
| /// Health check for Nats Server. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Relies on a static <see cref="ConnectionFactory"/> which provides factory methods to create | ||
| /// connections to NATS Servers, and a <see cref="IConnection"/> object connected to the NATS server. | ||
| /// </remarks> | ||
| public sealed class NatsHealthCheck : IHealthCheck, IDisposable | ||
| public sealed class NatsHealthCheck(INatsConnection connection) : IHealthCheck | ||
| { | ||
| private static readonly ConnectionFactory _connectionFactory = new(); | ||
|
|
||
| private readonly NatsOptions _options; | ||
| public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| await TryConnectAsync(connection).ConfigureAwait(false); | ||
|
|
||
| private IConnection? _connection; | ||
| var result = connection.ConnectionState switch | ||
Alirexaa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| NatsConnectionState.Open => HealthCheckResult.Healthy(), | ||
| NatsConnectionState.Connecting or NatsConnectionState.Reconnecting => HealthCheckResult.Degraded(), | ||
| NatsConnectionState.Closed => await TryConnectAsync(connection).ConfigureAwait(false), | ||
| _ => new HealthCheckResult(context.Registration.FailureStatus) | ||
| }; | ||
|
|
||
| public NatsHealthCheck(NatsOptions natsOptions) | ||
| { | ||
| _options = Guard.ThrowIfNull(natsOptions); | ||
| return result; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| private static async Task<HealthCheckResult> TryConnectAsync(INatsConnection natsConnection) | ||
| { | ||
| try | ||
| { | ||
| // Create new connection if there is no existing one | ||
| IConnection? connection = _connection; | ||
| if (connection == null) | ||
| { | ||
| #pragma warning disable IDISP001 // Dispose created [false positive, https://github.com/DotNetAnalyzers/IDisposableAnalyzers/issues/515] | ||
| connection = CreateConnection(_options); | ||
| #pragma warning restore IDISP001 // Dispose created | ||
| var exchanged = Interlocked.CompareExchange(ref _connection, connection, null); | ||
| if (exchanged != null) // was set by other thread | ||
| { | ||
| connection.Dispose(); | ||
| connection = exchanged; | ||
| } | ||
| } | ||
|
|
||
| // reset connection in case of stuck so the next HC call will establish it again | ||
| // https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/1544 | ||
| if (connection.State == ConnState.DISCONNECTED || connection.State == ConnState.CLOSED) | ||
| _connection = null; | ||
|
|
||
| var healthCheckResult = GetHealthCheckResultFromState(connection); | ||
| return Task.FromResult(healthCheckResult); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| var unhealthy = new HealthCheckResult(context.Registration.FailureStatus, exception: ex); | ||
| return Task.FromResult(unhealthy); | ||
| } | ||
|
|
||
| IConnection CreateConnection(NatsOptions options) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(options.CredentialsPath)) | ||
| return _connectionFactory.CreateConnection(options.Url, options.CredentialsPath); | ||
| if (!string.IsNullOrWhiteSpace(options.Jwt) && !string.IsNullOrWhiteSpace(options.PrivateNKey)) | ||
| return _connectionFactory.CreateConnection(options.Url, options.Jwt, options.PrivateNKey); | ||
| return _connectionFactory.CreateConnection(options.Url); | ||
| await natsConnection.ConnectAsync().ConfigureAwait(false); | ||
| return HealthCheckResult.Healthy(); | ||
| } | ||
|
|
||
| HealthCheckResult GetHealthCheckResultFromState(IConnection connection) | ||
| catch (Exception) | ||
| { | ||
| string description = GetDescription(connection); | ||
|
|
||
| return connection.State switch | ||
| { | ||
| ConnState.CONNECTED => HealthCheckResult.Healthy(description, GetStatsData(connection)), | ||
| ConnState.CONNECTING | ||
| or ConnState.RECONNECTING | ||
| or ConnState.DRAINING_SUBS | ||
| or ConnState.DRAINING_PUBS => HealthCheckResult.Degraded(description), | ||
| ConnState.CLOSED | ||
| or ConnState.DISCONNECTED => HealthCheckResult.Unhealthy(description), | ||
| _ => new HealthCheckResult(context.Registration.FailureStatus, description), | ||
| }; | ||
| return HealthCheckResult.Unhealthy(); | ||
| } | ||
|
|
||
| static string GetDescription(IConnection connection) | ||
| { | ||
| var sb = new StringBuilder(); | ||
| sb.AppendFormat("{0}: {1}; ", nameof(connection.ClientIP), connection.ClientIP); | ||
| if (!string.IsNullOrWhiteSpace(connection.ConnectedUrl)) | ||
| sb.AppendFormat("{0}: {1}; ", nameof(connection.ConnectedUrl), connection.ConnectedUrl); | ||
| sb.AppendFormat("{0}: {1}; ", nameof(connection.State), connection.State); | ||
| if (connection.SubscriptionCount != default) | ||
| sb.AppendFormat("{0}: {1}", nameof(connection.SubscriptionCount), connection.SubscriptionCount); | ||
| return sb.ToString(); | ||
| } | ||
|
|
||
| static IReadOnlyDictionary<string, object> GetStatsData(IConnection connection) => | ||
| new Dictionary<string, object> | ||
| { | ||
| [nameof(connection.Stats.InMsgs)] = connection.Stats.InMsgs, | ||
| [nameof(connection.Stats.OutMsgs)] = connection.Stats.OutMsgs, | ||
| [nameof(connection.Stats.InBytes)] = connection.Stats.InBytes, | ||
| [nameof(connection.Stats.OutBytes)] = connection.Stats.OutBytes, | ||
| [nameof(connection.Stats.Reconnects)] = connection.Stats.Reconnects | ||
| }; | ||
| } | ||
|
|
||
| public void Dispose() => _connection?.Dispose(); | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.