Skip to content

Commit

Permalink
fix: ref #623
Browse files Browse the repository at this point in the history
refactoring RollbarInfrastructure
  • Loading branch information
akornich committed Mar 12, 2022
1 parent 8c53bd3 commit 83cd67f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 88 deletions.
57 changes: 29 additions & 28 deletions Rollbar/Infrastructure/RollbarQueueController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@ internal sealed class RollbarQueueController
{
#region singleton implementation

//private static RollbarQueueController _singleton;

private static readonly Lazy<RollbarQueueController> lazy =
private static readonly Lazy<RollbarQueueController> lazySingleton =
new Lazy<RollbarQueueController>(() => 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;



/// <summary>
/// Gets the instance.
/// </summary>
Expand All @@ -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;
}
}

Expand All @@ -71,26 +91,7 @@ public static RollbarQueueController? Instance
/// </summary>
private RollbarQueueController()
{
}

/// <summary>
/// Class NestedSingleInstance. This class cannot be inherited.
/// </summary>
private sealed class NestedSingleInstance
{
/// <summary>
/// Prevents a default instance of the <see cref="NestedSingleInstance"/> class from being created.
/// </summary>
private NestedSingleInstance()
{
}

/// <summary>
/// The instance
/// </summary>
internal static readonly RollbarQueueController? TheInstance =
RollbarInfrastructure.Instance.IsInitialized ? new RollbarQueueController()
: null;
traceSource.TraceInformation($"Creating the {typeof(RollbarQueueController).Name}...");
}

#endregion singleton implementation
Expand Down
54 changes: 22 additions & 32 deletions Rollbar/RollbarInfrastructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
///
/// </summary>
/// <seealso cref="System.IDisposable" />
public class RollbarInfrastructure
public sealed class RollbarInfrastructure
: IRollbarInfrastructure
, IDisposable
{
Expand All @@ -38,11 +38,12 @@ public class RollbarInfrastructure

#region singleton implementation

//private static RollbarInfrastructure _singleton;

private static readonly Lazy<RollbarInfrastructure> lazy =
private static readonly Lazy<RollbarInfrastructure> lazySingleton =
new Lazy<RollbarInfrastructure>(() => new RollbarInfrastructure());

//private static readonly object classLock = new object();
//private static volatile RollbarInfrastructure? singleton;

/// <summary>
/// Gets the instance.
/// </summary>
Expand All @@ -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;
}
}

Expand All @@ -71,25 +76,6 @@ private RollbarInfrastructure()
traceSource.TraceInformation($"Creating the {typeof(RollbarInfrastructure).Name}...");
}

/// <summary>
/// Class NestedSingleInstance. This class cannot be inherited.
/// </summary>
private sealed class NestedSingleInstance
{
/// <summary>
/// Prevents a default instance of the <see cref="NestedSingleInstance"/> class from being created.
/// </summary>
private NestedSingleInstance()
{
}

/// <summary>
/// The instance
/// </summary>
internal static readonly RollbarInfrastructure TheInstance =
new RollbarInfrastructure();
}

#endregion singleton implementation

/// <summary>
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -264,7 +254,7 @@ private void CompleteProcessing()
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if(!disposedValue)
{
Expand Down
51 changes: 29 additions & 22 deletions Rollbar/Telemetry/RollbarTelemetryCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <summary>
/// Implements Rollbar telemetry collector service.
/// </summary>
internal class RollbarTelemetryCollector
internal sealed class RollbarTelemetryCollector
: IRollbarTelemetryCollector
, IDisposable
{
Expand All @@ -19,9 +19,18 @@ internal class RollbarTelemetryCollector

#region singleton implementation

private static readonly Lazy<RollbarTelemetryCollector> lazy =
private static readonly Lazy<RollbarTelemetryCollector> lazySingleton =
new Lazy<RollbarTelemetryCollector>(() => 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;

/// <summary>
/// Gets the instance.
/// </summary>
Expand All @@ -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;
}
}

Expand All @@ -47,20 +67,7 @@ public static RollbarTelemetryCollector? Instance
/// </summary>
private RollbarTelemetryCollector()
{
}

private sealed class NestedSingleInstance
{
private NestedSingleInstance()
{
}

/// <summary>
/// The singleton-like instance of the service.
/// </summary>
internal static readonly RollbarTelemetryCollector? TheInstance =
RollbarInfrastructure.Instance.IsInitialized ? new RollbarTelemetryCollector()
: null;
traceSource.TraceInformation($"Creating the {typeof(RollbarTelemetryCollector).Name}...");
}

#endregion singleton implementation
Expand Down Expand Up @@ -327,7 +334,7 @@ private void KeepCollectingTelemetry(object? data)
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (!disposedValue)
{
Expand Down
8 changes: 4 additions & 4 deletions Samples/Sample.VBNet.ConsoleApp/App.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83cd67f

Please sign in to comment.