fix: address MonitorServer review — dispose resources, add cancellation, improve test reliability

This commit is contained in:
Joseph Doherty
2026-02-22 22:30:14 -05:00
parent 63198ef83b
commit 818bc0ba1f
3 changed files with 27 additions and 10 deletions

View File

@@ -8,7 +8,7 @@ namespace NATS.Server.Monitoring;
/// Handles building the Varz response from server state and process metrics.
/// Corresponds to Go server/monitor.go handleVarz function.
/// </summary>
public sealed class VarzHandler
public sealed class VarzHandler : IDisposable
{
private readonly NatsServer _server;
private readonly NatsOptions _options;
@@ -21,17 +21,17 @@ public sealed class VarzHandler
{
_server = server;
_options = options;
var proc = Process.GetCurrentProcess();
using var proc = Process.GetCurrentProcess();
_lastCpuSampleTime = DateTime.UtcNow;
_lastCpuUsage = proc.TotalProcessorTime;
}
public async Task<Varz> HandleVarzAsync()
public async Task<Varz> HandleVarzAsync(CancellationToken ct = default)
{
await _varzMu.WaitAsync();
await _varzMu.WaitAsync(ct);
try
{
var proc = Process.GetCurrentProcess();
using var proc = Process.GetCurrentProcess();
var now = DateTime.UtcNow;
var uptime = now - _server.StartTime;
var stats = _server.Stats;
@@ -100,6 +100,11 @@ public sealed class VarzHandler
}
}
public void Dispose()
{
_varzMu.Dispose();
}
/// <summary>
/// Formats a TimeSpan as a human-readable uptime string matching Go server format.
/// </summary>