Skip to content

Commit

Permalink
improvement: unleash lifetime config visibility (#112)
Browse files Browse the repository at this point in the history
* count instances and log

* more info on registering as singleton in docs

* recommend strongly

Co-authored-by: Simon Hornby <liquidwicked64@gmail.com>
  • Loading branch information
daveleek and sighphyre committed Oct 24, 2022
1 parent dc72684 commit 0943a2d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ var settings = new UnleashSettings()
};

var unleash = new DefaultUnleash(settings);

// Add to Container as Singleton
// .NET Core 3/.NET 5/...
services.AddSingleton<IUnleash>(c => unleash);

```
Note that the `DefaultUnleash` constructor sets up the toggle caching and periodic background fetching. If you want the cache to be populated immediately, see the [synchronous startup](#synchronous-startup) section
Unleash will attempt to log a warning for each new instance after instance count reaches 10.
Also note that the `DefaultUnleash` constructor sets up the toggle caching and periodic background fetching. If you want the cache to be populated immediately, see the [synchronous startup](#synchronous-startup) section

When your application shuts down, remember to dispose the unleash instance.

Expand Down
16 changes: 15 additions & 1 deletion src/Unleash/DefaultUnleash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ namespace Unleash
using Internal;
using Logging;
using Strategies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Unleash.Variants;

/// <inheritdoc />
Expand All @@ -14,6 +16,10 @@ public class DefaultUnleash : IUnleash

private static readonly UnknownStrategy UnknownStrategy = new UnknownStrategy();

private static int InitializedInstanceCount = 0;

private const int WarnOnInstanceCount = 10;

private static readonly IStrategy[] DefaultStrategies = {
new DefaultStrategy(),
new UserWithIdStrategy(),
Expand Down Expand Up @@ -48,6 +54,8 @@ public DefaultUnleash(UnleashSettings settings, params IStrategy[] strategies)
///// <param name="strategies">Custom strategies.</param>
public DefaultUnleash(UnleashSettings settings, bool overrideDefaultStrategies, params IStrategy[] strategies)
{
var currentInstanceNo = Interlocked.Increment(ref InitializedInstanceCount);

this.settings = settings;

var settingsValidator = new UnleashSettingsValidator();
Expand All @@ -58,7 +66,13 @@ public DefaultUnleash(UnleashSettings settings, bool overrideDefaultStrategies,

services = new UnleashServices(settings, strategyMap);

Logger.Info($"UNLEASH: Unleash is initialized and configured with: {settings}");
Logger.Info($"UNLEASH: Unleash instance number { currentInstanceNo } is initialized and configured with: {settings}");

if (currentInstanceNo >= WarnOnInstanceCount)
{
Logger.Warn($"UNLEASH: Unleash instance count for this process is now {currentInstanceNo}.");
Logger.Warn("Ideally you should only need 1 instance of Unleash per app/process, we strongly recommend setting up Unleash as a singleton.");
}
}

/// <inheritdoc />
Expand Down

0 comments on commit 0943a2d

Please sign in to comment.