Skip to content

Commit

Permalink
Adds FilterIndex to FeatureFilterEvaluationContext. Uses it in the ca…
Browse files Browse the repository at this point in the history
…che key.
  • Loading branch information
rossgrambo committed May 13, 2023
1 parent 541bfc0 commit c44de57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class FeatureFilterEvaluationContext
/// </summary>
public string FeatureName { get; set; }

/// <summary>
/// The index of the feature filter this context is being used for.
/// </summary>
public int FilterIndex { get; set; }

/// <summary>
/// The settings provided for the feature filter to use when evaluating whether the feature should be enabled.
/// </summary>
Expand Down
15 changes: 12 additions & 3 deletions src/Microsoft.FeatureManagement/FeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,16 @@ private async Task<bool> IsEnabledAsync<TContext>(string feature, TContext appCo
// We iterate until we hit our target evaluation
bool targetEvaluation = !enabled;

//
// Keep track of the index of the filter we are evaluating
int filterIndex = -1;

//
// For all enabling filters listed in the feature's state, evaluate them according to requirement type
foreach (FeatureFilterConfiguration featureFilterConfiguration in featureDefinition.EnabledFor)
{
filterIndex++;

//
// Handle AlwaysOn filters
if (string.Equals(featureFilterConfiguration.Name, "AlwaysOn", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -151,6 +157,7 @@ private async Task<bool> IsEnabledAsync<TContext>(string feature, TContext appCo
var context = new FeatureFilterEvaluationContext()
{
FeatureName = feature,
FilterIndex = filterIndex,
Parameters = featureFilterConfiguration.Parameters
};

Expand Down Expand Up @@ -220,18 +227,20 @@ private void BindSettings(IFeatureFilterMetadata filter, FeatureFilterEvaluation
object settings;

ConfigurationCacheItem cacheItem;


string cacheKey = $"{context.FeatureName}.{context.FilterIndex}";

//
// Check if settings already bound from configuration or the parameters have changed
if (!_parametersCache.TryGetValue(context.FeatureName, out cacheItem) ||
if (!_parametersCache.TryGetValue(cacheKey, out cacheItem) ||
cacheItem.Parameters != context.Parameters)
{
settings = binder.BindParameters(context.Parameters);

if (_featureDefinitionProvider is IFeatureDefinitionProviderCacheable)
{
_parametersCache.Set(
context.FeatureName,
cacheKey,
new ConfigurationCacheItem
{
Settings = settings,
Expand Down

0 comments on commit c44de57

Please sign in to comment.