fix: address MonitorServer review — dispose resources, add cancellation, improve test reliability
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user