Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support concurrency in IFeatureManagerSnapshot #141

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions src/Microsoft.FeatureManagement/FeatureManagerSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.
//
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand All @@ -13,7 +14,7 @@ namespace Microsoft.FeatureManagement
class FeatureManagerSnapshot : IFeatureManagerSnapshot
{
private readonly IFeatureManager _featureManager;
private readonly IDictionary<string, bool> _flagCache = new Dictionary<string, bool>();
private readonly ConcurrentDictionary<string, Task<bool>> _flagCache = new ConcurrentDictionary<string, Task<bool>>();
private IEnumerable<string> _featureNames;

public FeatureManagerSnapshot(IFeatureManager featureManager)
Expand Down Expand Up @@ -41,36 +42,18 @@ await foreach (string featureName in _featureManager.GetFeatureNamesAsync().Conf
}
}

public async Task<bool> IsEnabledAsync(string feature)
public Task<bool> IsEnabledAsync(string feature)
{
//
// First, check local cache
if (_flagCache.ContainsKey(feature))
{
return _flagCache[feature];
}

bool enabled = await _featureManager.IsEnabledAsync(feature).ConfigureAwait(false);

_flagCache[feature] = enabled;

return enabled;
return _flagCache.GetOrAdd(
feature,
(key) => _featureManager.IsEnabledAsync(key));
jimmyca15 marked this conversation as resolved.
Show resolved Hide resolved
}

public async Task<bool> IsEnabledAsync<TContext>(string feature, TContext context)
public Task<bool> IsEnabledAsync<TContext>(string feature, TContext context)
{
//
// First, check local cache
if (_flagCache.ContainsKey(feature))
{
return _flagCache[feature];
}

bool enabled = await _featureManager.IsEnabledAsync(feature, context).ConfigureAwait(false);

_flagCache[feature] = enabled;

return enabled;
return _flagCache.GetOrAdd(
feature,
(key) => _featureManager.IsEnabledAsync(key, context));
}
}
}
66 changes: 59 additions & 7 deletions tests/Tests.FeatureManagement/FeatureManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task ReadsConfiguration()

Assert.Equal(ConditionalFeature, evaluationContext.FeatureName);

return true;
return Task.FromResult(true);
};

await featureManager.IsEnabledAsync(ConditionalFeature);
Expand Down Expand Up @@ -106,14 +106,14 @@ public async Task Integrates()

TestFilter testFeatureFilter = (TestFilter)featureFilters.First(f => f is TestFilter);

testFeatureFilter.Callback = _ => true;
testFeatureFilter.Callback = _ => Task.FromResult(true);

HttpResponseMessage res = await testServer.CreateClient().GetAsync("");

Assert.True(res.Headers.Contains(nameof(MvcFilter)));
Assert.True(res.Headers.Contains(nameof(RouterMiddleware)));

testFeatureFilter.Callback = _ => false;
testFeatureFilter.Callback = _ => Task.FromResult(false);

res = await testServer.CreateClient().GetAsync("");

Expand Down Expand Up @@ -143,7 +143,7 @@ public async Task GatesFeatures()

//
// Enable all features
testFeatureFilter.Callback = ctx => true;
testFeatureFilter.Callback = ctx => Task.FromResult(true);

HttpResponseMessage gateAllResponse = await testServer.CreateClient().GetAsync("gateAll");
HttpResponseMessage gateAnyResponse = await testServer.CreateClient().GetAsync("gateAny");
Expand All @@ -153,7 +153,7 @@ public async Task GatesFeatures()

//
// Enable 1/2 features
testFeatureFilter.Callback = ctx => ctx.FeatureName == Enum.GetName(typeof(Features), Features.ConditionalFeature);
testFeatureFilter.Callback = ctx => Task.FromResult(ctx.FeatureName == Enum.GetName(typeof(Features), Features.ConditionalFeature));

gateAllResponse = await testServer.CreateClient().GetAsync("gateAll");
gateAnyResponse = await testServer.CreateClient().GetAsync("gateAny");
Expand All @@ -163,7 +163,7 @@ public async Task GatesFeatures()

//
// Enable no
testFeatureFilter.Callback = ctx => false;
testFeatureFilter.Callback = ctx => Task.FromResult(false);

gateAllResponse = await testServer.CreateClient().GetAsync("gateAll");
gateAnyResponse = await testServer.CreateClient().GetAsync("gateAny");
Expand Down Expand Up @@ -555,14 +555,66 @@ public async Task CustomFeatureDefinitionProvider()

Assert.Equal(ConditionalFeature, evaluationContext.FeatureName);

return true;
return Task.FromResult(true);
};

await featureManager.IsEnabledAsync(ConditionalFeature);

Assert.True(called);
}

[Fact]
public async Task ThreadsafeSnapshot()
{
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

var services = new ServiceCollection();

services
.AddSingleton(config)
.AddFeatureManagement()
.AddFeatureFilter<TestFilter>();

ServiceProvider serviceProvider = services.BuildServiceProvider();

IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManagerSnapshot>();

IEnumerable<IFeatureFilterMetadata> featureFilters = serviceProvider.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>();

//
// Sync filter
TestFilter testFeatureFilter = (TestFilter)featureFilters.First(f => f is TestFilter);

bool called = false;

testFeatureFilter.Callback = async (evaluationContext) =>
{
called = true;

await Task.Delay(10);

return new Random().Next(0, 100) > 50;
};

var tasks = new List<Task<bool>>();

for (int i = 0; i < 1000; i++)
{
tasks.Add(featureManager.IsEnabledAsync(ConditionalFeature));
}

Assert.True(called);

await Task.WhenAll(tasks);

bool result = tasks.First().Result;

foreach (Task<bool> t in tasks)
{
Assert.Equal(result, t.Result);
}
}

private static void DisableEndpointRouting(MvcOptions options)
{
#if NET5_0 || NETCOREAPP3_1
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests.FeatureManagement/TestFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Tests.FeatureManagement
{
class TestFilter : IFeatureFilter
{
public Func<FeatureFilterEvaluationContext, bool> Callback { get; set; }
public Func<FeatureFilterEvaluationContext, Task<bool>> Callback { get; set; }

public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context)
{
return Task.FromResult(Callback?.Invoke(context) ?? false);
return Callback?.Invoke(context) ?? Task.FromResult(false);
}
}
}