Skip to content

Commit

Permalink
chore: Dispose() refactoring (#2798)
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr authored Oct 4, 2024
1 parent 709ddb6 commit 5059eae
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public AgentHealthReporter(IMetricBuilder metricBuilder, IScheduler scheduler)

public override void Dispose()
{
base.Dispose();
_scheduler.StopExecuting(LogPeriodicReport);
base.Dispose();
}

private void LogPeriodicReport()
Expand All @@ -78,7 +78,7 @@ private void LogPeriodicReport()
}
}
var message = events.Count > 0 ? string.Join(", ", events) : "No events";
Log.Info($"In the last {_timeBetweenExecutions.TotalMinutes} minutes: {message}");
Log.Info($"AgentHealthReporter: In the last {_timeBetweenExecutions.TotalMinutes} minutes: {message}");
}

public void ReportSupportabilityCountMetric(string metricName, long count = 1)
Expand Down
8 changes: 5 additions & 3 deletions src/Agent/NewRelic/Agent/Core/AgentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private void Shutdown(bool cleanShutdown)

try
{
Log.Debug("Starting the shutdown process for the .NET Agent.");
Log.Info("Starting the shutdown process for the .NET Agent.");

AgentInitializer.OnExit -= ProcessExit;

Expand All @@ -408,15 +408,17 @@ private void Shutdown(bool cleanShutdown)

Log.Debug("Shutting down public agent services...");
StopServices();
Log.Info("The New Relic .NET Agent v{0} has shutdown (pid {1}) on app domain '{2}'", AgentInstallConfiguration.AgentVersion, AgentInstallConfiguration.ProcessId, AgentInstallConfiguration.AppDomainAppVirtualPath ?? AgentInstallConfiguration.AppDomainName);
}
catch (Exception e)
{
Log.Debug(e, "Shutdown error");
Log.Info(e, "Unexpected exception during agent shutdown");
}
finally
{
Log.Debug("Shutting down internal agent services...");
Dispose();

Log.Info("The New Relic .NET Agent v{Version} has shutdown (pid {pid}) on app domain '{appDomain}'", AgentInstallConfiguration.AgentVersion, AgentInstallConfiguration.ProcessId, AgentInstallConfiguration.AppDomainAppVirtualPath ?? AgentInstallConfiguration.AppDomainName);
Serilog.Log.CloseAndFlush();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ private void OnPreCleanShutdown(PreCleanShutdownEvent obj)

public override void Dispose()
{
base.Dispose();
_scheduler.StopExecuting(Harvest);
base.Dispose();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public CustomEventAggregator(IDataTransportService dataTransportService, ISchedu

public override void Dispose()
{
base.Dispose();
_readerWriterLockSlim.Dispose();

base.Dispose();
}

protected override TimeSpan HarvestCycle => _configuration.CustomEventsHarvestCycle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public ErrorEventAggregator(IDataTransportService dataTransportService, ISchedul

public override void Dispose()
{
base.Dispose();
_readerWriterLock.Dispose();
base.Dispose();
}

public override void Collect(ErrorEventWireModel errorEventWireModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public ErrorTraceAggregator(IDataTransportService dataTransportService, ISchedul

public override void Dispose()
{
base.Dispose();
_readerWriterLock.Dispose();
base.Dispose();
}

protected override TimeSpan HarvestCycle => _configuration.ErrorTracesHarvestCycle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public LogEventAggregator(IDataTransportService dataTransportService, IScheduler
protected override TimeSpan HarvestCycle => _configuration.LogEventsHarvestCycle;
protected override bool IsEnabled => _configuration.LogEventCollectorEnabled;

public override void Dispose()
{
base.Dispose();
}

public override void Collect(LogEventWireModel loggingEventWireModel)
{
_agentHealthReporter.ReportLoggingEventCollected();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public SpanEventAggregator(IDataTransportService dataTransportService, ISchedule

public override void Dispose()
{
base.Dispose();
_readerWriterLockSlim.Dispose();
base.Dispose();
}

public override void Collect(ISpanEventWireModel wireModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public TransactionEventAggregator(IDataTransportService dataTransportService, IS

public override void Dispose()
{
base.Dispose();
_readerWriterLock.Dispose();
base.Dispose();
}

public override void Collect(TransactionEventWireModel transactionEventWireModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ public override IHttpResponse Send(IHttpRequest request)
}
}

public override void Dispose()
{
base.Dispose();
}

// for unit testing only
public void SetHttpWebRequestFunc(Func<Uri, HttpWebRequest> getHttpWebRequestFunc )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AgentContainer : IContainer

// use the scope instead of the container to resolve instances. This allows us to replace registrations in a new scope for unit testing
private ILifetimeScope _scope;
private bool _disposedValue;
private bool _disposed;
private readonly Dictionary<Type, object> _registrationsToReplace = new Dictionary<Type, object>();

public AgentContainer()
Expand Down Expand Up @@ -102,7 +102,7 @@ public IEnumerable<T> ResolveAll<T>()

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
if (!_disposed)
{
if (disposing)
{
Expand All @@ -113,7 +113,7 @@ protected virtual void Dispose(bool disposing)
_container = null;
}

_disposedValue = true;
_disposed = true;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Agent/NewRelic/Agent/Core/Samplers/AbstractSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ protected AbstractSampler(IScheduler scheduler, TimeSpan frequency)

public override void Dispose()
{
base.Dispose();
Stop();
base.Dispose();
}

protected override void OnConfigurationUpdated(ConfigurationUpdateSource configurationUpdateSource)
Expand Down
3 changes: 2 additions & 1 deletion src/Agent/NewRelic/Agent/Core/Samplers/GCSamplerNetCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ protected override void Stop()

public override void Dispose()
{
base.Dispose();
_listener?.StopListening();
_listener?.Dispose();
_listener = null;

base.Dispose();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Agent/NewRelic/Agent/Core/Samplers/ThreadStatsSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ protected override void Stop()

public override void Dispose()
{
base.Dispose();
_listener?.StopListening();
#if NETFRAMEWORK // calling .Dispose() in .NET 7 explodes. No idea why.
_listener?.Dispose();
#endif
_listener = null;

base.Dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public interface IPerformanceCounterProxy : IDisposable

public class PerformanceCounterProxy : IPerformanceCounterProxy
{
private readonly PerformanceCounter _counter;
private bool _counterIsDisposed = false;
private PerformanceCounter _counter;

public PerformanceCounterProxy(string categoryName, string counterName, string instanceName)
{
Expand All @@ -32,10 +31,10 @@ public float NextValue()

public void Dispose()
{
if (_counter != null && !_counterIsDisposed)
if (_counter != null)
{
_counter.Dispose();
_counterIsDisposed = true;
_counter = null;
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/Agent/NewRelic/Agent/Core/Time/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ public void Dispose()

foreach (var timer in _recurringTimers.Values)
{
if (timer != null)
{
timer.Dispose();
}
timer?.Dispose();
}
_recurringTimers.Clear();
}
Expand All @@ -219,8 +216,7 @@ private class TimerStatus : IDisposable
public void Dispose()
{
var timer = Timer;
if (timer != null)
timer.Dispose();
timer?.Dispose();
}
}
}
Expand Down

0 comments on commit 5059eae

Please sign in to comment.