From 83cd67f63981cc5687a9555e1be7d5c6265e516a Mon Sep 17 00:00:00 2001 From: Andrey Kornich Date: Fri, 11 Mar 2022 16:14:51 -0800 Subject: [PATCH] fix: ref #623 refactoring RollbarInfrastructure --- .../Infrastructure/RollbarQueueController.cs | 57 ++++++++++--------- Rollbar/RollbarInfrastructure.cs | 54 +++++++----------- .../Telemetry/RollbarTelemetryCollector.cs | 51 ++++++++++------- Samples/Sample.VBNet.ConsoleApp/App.config | 8 +-- .../My Project/Resources.Designer.vb | 2 +- .../My Project/Settings.Designer.vb | 2 +- 6 files changed, 86 insertions(+), 88 deletions(-) diff --git a/Rollbar/Infrastructure/RollbarQueueController.cs b/Rollbar/Infrastructure/RollbarQueueController.cs index bc7d1fc5..d7e890e7 100644 --- a/Rollbar/Infrastructure/RollbarQueueController.cs +++ b/Rollbar/Infrastructure/RollbarQueueController.cs @@ -41,11 +41,20 @@ internal sealed class RollbarQueueController { #region singleton implementation - //private static RollbarQueueController _singleton; - - private static readonly Lazy lazy = + private static readonly Lazy lazySingleton = new Lazy(() => new RollbarQueueController()); + //private static bool allowSingleton = false; + //internal static void AllowSingleton() + //{ + // allowSingleton = true; + //} + + //private static readonly object classLock = new object(); + //private static volatile RollbarQueueController? singleton; + + + /// /// Gets the instance. /// @@ -54,15 +63,26 @@ public static RollbarQueueController? Instance { get { - //return NestedSingleInstance.TheInstance; + if (!RollbarInfrastructure.Instance.IsInitialized) + { + return null; + } - //if (_singleton == null) + //if (singleton == null) //{ - // _singleton = new RollbarQueueController(); + // lock (classLock) + // { + // if (singleton == null) + // { + // singleton = new RollbarQueueController(); + // } + // } //} - //return _singleton; + //return singleton; + + //return allowSingleton ? lazySingleton.Value : null; - return RollbarInfrastructure.Instance.IsInitialized ? lazy.Value : null; + return lazySingleton.Value; } } @@ -71,26 +91,7 @@ public static RollbarQueueController? Instance /// private RollbarQueueController() { - } - - /// - /// Class NestedSingleInstance. This class cannot be inherited. - /// - private sealed class NestedSingleInstance - { - /// - /// Prevents a default instance of the class from being created. - /// - private NestedSingleInstance() - { - } - - /// - /// The instance - /// - internal static readonly RollbarQueueController? TheInstance = - RollbarInfrastructure.Instance.IsInitialized ? new RollbarQueueController() - : null; + traceSource.TraceInformation($"Creating the {typeof(RollbarQueueController).Name}..."); } #endregion singleton implementation diff --git a/Rollbar/RollbarInfrastructure.cs b/Rollbar/RollbarInfrastructure.cs index 9ce23f28..d42f09d2 100644 --- a/Rollbar/RollbarInfrastructure.cs +++ b/Rollbar/RollbarInfrastructure.cs @@ -20,7 +20,7 @@ /// /// /// - public class RollbarInfrastructure + public sealed class RollbarInfrastructure : IRollbarInfrastructure , IDisposable { @@ -38,11 +38,12 @@ public class RollbarInfrastructure #region singleton implementation - //private static RollbarInfrastructure _singleton; - - private static readonly Lazy lazy = + private static readonly Lazy lazySingleton = new Lazy(() => new RollbarInfrastructure()); + //private static readonly object classLock = new object(); + //private static volatile RollbarInfrastructure? singleton; + /// /// Gets the instance. /// @@ -51,15 +52,19 @@ public static RollbarInfrastructure Instance { get { - //return NestedSingleInstance.TheInstance; - - //if (_singleton == null) + //if (singleton == null) //{ - // _singleton = new RollbarQueueController(); + // lock (classLock) + // { + // if (singleton == null) + // { + // singleton = new RollbarInfrastructure(); + // } + // } //} - //return _singleton; + //return singleton; - return lazy.Value; + return lazySingleton.Value; } } @@ -71,25 +76,6 @@ private RollbarInfrastructure() traceSource.TraceInformation($"Creating the {typeof(RollbarInfrastructure).Name}..."); } - /// - /// Class NestedSingleInstance. This class cannot be inherited. - /// - private sealed class NestedSingleInstance - { - /// - /// Prevents a default instance of the class from being created. - /// - private NestedSingleInstance() - { - } - - /// - /// The instance - /// - internal static readonly RollbarInfrastructure TheInstance = - new RollbarInfrastructure(); - } - #endregion singleton implementation /// @@ -165,6 +151,9 @@ public IRollbarInfrastructureConfig Config public void Init(IRollbarInfrastructureConfig config) { Assumption.AssertNotNull(config, nameof(config)); + Assumption.AssertNotNull(RollbarInfrastructure.Instance, nameof(RollbarInfrastructure.Instance)); + Assumption.AssertFalse(RollbarInfrastructure.Instance.IsInitialized, nameof(RollbarInfrastructure.Instance.IsInitialized)); + lock(this._syncLock) { @@ -186,9 +175,10 @@ public void Init(IRollbarInfrastructureConfig config) this.ValidateConfiguration(); this._config.Reconfigured += _config_Reconfigured; this._initializedOnce = true; + Assumption.AssertTrue(RollbarInfrastructure.Instance.IsInitialized, nameof(RollbarInfrastructure.Instance.IsInitialized)); - RollbarQueueController.Instance!.Init(config); - RollbarTelemetryCollector.Instance!.Init(config.RollbarTelemetryOptions); + RollbarQueueController.Instance?.Init(config); + RollbarTelemetryCollector.Instance?.Init(config.RollbarTelemetryOptions); // NOTE: RollbarConfig // - init ConnectivityMonitor service as needed // NOTE: It should be sufficient to make ConnectivityMonitor as internal class @@ -264,7 +254,7 @@ private void CompleteProcessing() /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool disposing) + private void Dispose(bool disposing) { if(!disposedValue) { diff --git a/Rollbar/Telemetry/RollbarTelemetryCollector.cs b/Rollbar/Telemetry/RollbarTelemetryCollector.cs index 017e5f74..41d8f762 100644 --- a/Rollbar/Telemetry/RollbarTelemetryCollector.cs +++ b/Rollbar/Telemetry/RollbarTelemetryCollector.cs @@ -10,7 +10,7 @@ /// /// Implements Rollbar telemetry collector service. /// - internal class RollbarTelemetryCollector + internal sealed class RollbarTelemetryCollector : IRollbarTelemetryCollector , IDisposable { @@ -19,9 +19,18 @@ internal class RollbarTelemetryCollector #region singleton implementation - private static readonly Lazy lazy = + private static readonly Lazy lazySingleton = new Lazy(() => new RollbarTelemetryCollector()); + //private static bool allowSingleton = false; + //internal static void AllowSingleton() + //{ + // allowSingleton = true; + //} + + //private static readonly object classLock = new object(); + //private static volatile RollbarTelemetryCollector? singleton; + /// /// Gets the instance. /// @@ -30,15 +39,26 @@ public static RollbarTelemetryCollector? Instance { get { - //return NestedSingleInstance.TheInstance; + if (!RollbarInfrastructure.Instance.IsInitialized) + { + return null; + } - //if (_singleton == null) + //if (singleton == null) //{ - // _singleton = new RollbarQueueController(); + // lock (classLock) + // { + // if (singleton == null) + // { + // singleton = new RollbarTelemetryCollector(); + // } + // } //} - //return _singleton; + //return singleton; + + //return allowSingleton ? lazySingleton.Value : null; - return RollbarInfrastructure.Instance.IsInitialized ? lazy.Value : null; + return lazySingleton.Value; } } @@ -47,20 +67,7 @@ public static RollbarTelemetryCollector? Instance /// private RollbarTelemetryCollector() { - } - - private sealed class NestedSingleInstance - { - private NestedSingleInstance() - { - } - - /// - /// The singleton-like instance of the service. - /// - internal static readonly RollbarTelemetryCollector? TheInstance = - RollbarInfrastructure.Instance.IsInitialized ? new RollbarTelemetryCollector() - : null; + traceSource.TraceInformation($"Creating the {typeof(RollbarTelemetryCollector).Name}..."); } #endregion singleton implementation @@ -327,7 +334,7 @@ private void KeepCollectingTelemetry(object? data) /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool disposing) + private void Dispose(bool disposing) { if (!disposedValue) { diff --git a/Samples/Sample.VBNet.ConsoleApp/App.config b/Samples/Sample.VBNet.ConsoleApp/App.config index bcb2ae2d..a58a46ef 100644 --- a/Samples/Sample.VBNet.ConsoleApp/App.config +++ b/Samples/Sample.VBNet.ConsoleApp/App.config @@ -1,13 +1,13 @@ - + - + - - + + diff --git a/Samples/Sample.VBNet.ConsoleApp/My Project/Resources.Designer.vb b/Samples/Sample.VBNet.ConsoleApp/My Project/Resources.Designer.vb index 9608190f..b64fba16 100644 --- a/Samples/Sample.VBNet.ConsoleApp/My Project/Resources.Designer.vb +++ b/Samples/Sample.VBNet.ConsoleApp/My Project/Resources.Designer.vb @@ -22,7 +22,7 @@ Namespace My.Resources ''' ''' A strongly-typed resource class, for looking up localized strings, etc. ''' - _ diff --git a/Samples/Sample.VBNet.ConsoleApp/My Project/Settings.Designer.vb b/Samples/Sample.VBNet.ConsoleApp/My Project/Settings.Designer.vb index 1e3cf95f..9eeb56e9 100644 --- a/Samples/Sample.VBNet.ConsoleApp/My Project/Settings.Designer.vb +++ b/Samples/Sample.VBNet.ConsoleApp/My Project/Settings.Designer.vb @@ -15,7 +15,7 @@ Option Explicit On Namespace My _ Partial Friend NotInheritable Class MySettings Inherits Global.System.Configuration.ApplicationSettingsBase