Skip to content

Commit

Permalink
chore: swap out leaky yggdrasil custom strategy for an internal abstr…
Browse files Browse the repository at this point in the history
…action (#256)
  • Loading branch information
sighphyre authored Nov 5, 2024
1 parent 3f2b80c commit 3ec6702
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
18 changes: 15 additions & 3 deletions samples/DotnetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Unleash;
using Unleash.ClientFactory;
using Unleash.Internal;
using Unleash.Strategies;

var settings = new UnleashSettings()
{
Expand All @@ -18,7 +18,7 @@
Console.WriteLine("Starting Unleash SDK");

var unleashFactory = new UnleashClientFactory();
IUnleash unleash = await unleashFactory.CreateClientAsync(settings, synchronousInitialization: true);
IUnleash unleash = await unleashFactory.CreateClientAsync(settings, synchronousInitialization: true, new MyCustomStrategy());


while (true)
Expand All @@ -28,4 +28,16 @@

Console.WriteLine($"Toggle enabled: {enabled}, variant: {System.Text.Json.JsonSerializer.Serialize(variant)}");
await Task.Delay(1000);
}
}

// If you want to test this, you'll need to setup a custom strategy in your
// Unleash UI and add it to the 'test' toggle.
class MyCustomStrategy : IStrategy
{
public string Name => "my-custom-strategy";

public bool IsEnabled(Dictionary<string, string> parameters, UnleashContext context)
{
return true;
}
}
2 changes: 1 addition & 1 deletion src/Unleash/ClientFactory/IUnleashClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Threading.Tasks;
using Yggdrasil;
using Unleash.Strategies;

namespace Unleash.ClientFactory
{
Expand Down
6 changes: 3 additions & 3 deletions src/Unleash/ClientFactory/UnleashClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Yggdrasil;
using Unleash.Strategies;

namespace Unleash.ClientFactory
{
Expand All @@ -14,7 +14,7 @@ public class UnleashClientFactory : IUnleashClientFactory
TaskScheduler.Default);

/// <summary>
/// Initializes a new instance of Unleash client.
/// Initializes a new instance of Unleash client.
/// </summary>
/// <param name="synchronousInitialization">If true, fetch and cache toggles before returning. If false, allow the unleash client schedule an initial poll of features in the background</param>
/// <param name="strategies">Custom strategies, added in addtion to builtIn strategies.</param>
Expand All @@ -38,7 +38,7 @@ public IUnleash CreateClient(UnleashSettings settings, bool synchronousInitializ


/// <summary>
/// Initializes a new instance of Unleash client.
/// Initializes a new instance of Unleash client.
/// </summary>
/// <param name="synchronousInitialization">If true, fetch and cache toggles before returning. If false, allow the unleash client schedule an initial poll of features in the background</param>
/// <param name="strategies">Custom strategies, added in addtion to builtIn strategies.</param>
Expand Down
4 changes: 2 additions & 2 deletions src/Unleash/DefaultUnleash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ namespace Unleash
using Internal;
using Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Unleash.Strategies;
using Unleash.Utilities;

/// <inheritdoc />
Expand All @@ -29,7 +29,7 @@ public class DefaultUnleash : IUnleash
///// </summary>
///// <param name="config">Unleash settings</param>
///// <param name="strategies">Custom strategies.</param>
public DefaultUnleash(UnleashSettings settings, params Yggdrasil.IStrategy[] strategies)
public DefaultUnleash(UnleashSettings settings, params IStrategy[] strategies)
{
var currentInstanceNo = Interlocked.Increment(ref InitializedInstanceCount);

Expand Down
7 changes: 5 additions & 2 deletions src/Unleash/Internal/UnleashServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Unleash.Internal;
using Unleash.Logging;
using Unleash.Scheduling;
using Unleash.Strategies;
using Yggdrasil;

namespace Unleash
Expand Down Expand Up @@ -35,14 +36,16 @@ internal class UnleashServices : IDisposable
"userWithId"
};

public UnleashServices(UnleashSettings settings, EventCallbackConfig eventConfig, List<IStrategy> strategies = null)
public UnleashServices(UnleashSettings settings, EventCallbackConfig eventConfig, List<Strategies.IStrategy> strategies = null)
{
if (settings.FileSystem == null)
{
settings.FileSystem = new FileSystem(settings.Encoding);
}

engine = new YggdrasilEngine(strategies);
List<Yggdrasil.IStrategy> yggdrasilStrategies = strategies?.Select(s => new CustomStrategyAdapter(s)).Cast<Yggdrasil.IStrategy>().ToList();

engine = new YggdrasilEngine(yggdrasilStrategies);

var backupFile = settings.GetFeatureToggleFilePath();
var etagBackupFile = settings.GetFeatureToggleETagFilePath();
Expand Down
53 changes: 53 additions & 0 deletions src/Unleash/Strategies/IStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using Yggdrasil;


namespace Unleash.Strategies
{
using System.Collections.Generic;

/// <summary>
/// Defines a strategy for enabling a feature.
/// </summary>
public interface IStrategy
{
/// <summary>
/// Gets the strategy name
/// </summary>
string Name { get; }

/// <summary>
/// Calculates if the strategy is enabled for a given context
/// </summary>
bool IsEnabled(Dictionary<string, string> parameters, UnleashContext context);
}

internal class CustomStrategyAdapter : Yggdrasil.IStrategy
{
private IStrategy strategy { get; }

public CustomStrategyAdapter(IStrategy strategy)
{
this.strategy = strategy;
}

public string Name => strategy.Name;

public bool IsEnabled(Dictionary<string, string> parameters, Context context)
{
var currentTime = context.CurrentTime ?? DateTimeOffset.UtcNow;

var unleashContext = new UnleashContext.Builder()
.AppName(context.AppName)
.CurrentTime(currentTime)
.Environment(context.Environment)
.UserId(context.UserId)
.SessionId(context.SessionId)
.RemoteAddress(context.RemoteAddress)
.Build();
unleashContext.Properties = context.Properties;

return strategy.IsEnabled(parameters, unleashContext);
}
}
}

0 comments on commit 3ec6702

Please sign in to comment.