From 932e0368203f3480cd3deb27643cefc6512e9fc4 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Fri, 3 Jun 2022 23:40:42 +0300 Subject: [PATCH] Fix for memory leak in NATS (#1217) --- samples/HealthChecks.UI.Branding/Startup.cs | 3 +-- .../DependencyInjection/NatsHealthCheckBuilderExtensions.cs | 5 ++++- src/HealthChecks.Nats/NatsHealthCheck.cs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/samples/HealthChecks.UI.Branding/Startup.cs b/samples/HealthChecks.UI.Branding/Startup.cs index dc44f3874a..8cd5ef12ec 100644 --- a/samples/HealthChecks.UI.Branding/Startup.cs +++ b/samples/HealthChecks.UI.Branding/Startup.cs @@ -79,8 +79,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } } - public class RandomHealthCheck - : IHealthCheck + public class RandomHealthCheck : IHealthCheck { public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { diff --git a/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs b/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs index 378ad66099..653b0948ba 100644 --- a/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs @@ -39,9 +39,12 @@ public static IHealthChecksBuilder AddNats( var options = new NatsOptions(); setup?.Invoke(options); + builder.Services + .AddSingleton(sp => new NatsHealthCheck(options)); + return builder.Add(new HealthCheckRegistration( name ?? NAME, - sp => new NatsHealthCheck(options), + sp => sp.GetRequiredService(), failureStatus, tags, timeout)); diff --git a/src/HealthChecks.Nats/NatsHealthCheck.cs b/src/HealthChecks.Nats/NatsHealthCheck.cs index 811f8ee7b5..14dd205b1e 100644 --- a/src/HealthChecks.Nats/NatsHealthCheck.cs +++ b/src/HealthChecks.Nats/NatsHealthCheck.cs @@ -13,7 +13,7 @@ namespace HealthChecks.Nats /// public sealed class NatsHealthCheck : IHealthCheck, IDisposable { - private static readonly ConnectionFactory _connectionFactory = new ConnectionFactory(); + private static readonly ConnectionFactory _connectionFactory = new(); private readonly NatsOptions _options;