Skip to content

feat: Add caching to GoFeatureFlag provider #386

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using OpenFeature.Constant;
using OpenFeature.Contrib.Providers.GOFeatureFlag.converters;
using OpenFeature.Contrib.Providers.GOFeatureFlag.exception;
using OpenFeature.Contrib.Providers.GOFeatureFlag.extensions;
using OpenFeature.Contrib.Providers.GOFeatureFlag.hooks;
using OpenFeature.Contrib.Providers.GOFeatureFlag.models;
using OpenFeature.Model;
using ZiggyCreatures.Caching.Fusion;

namespace OpenFeature.Contrib.Providers.GOFeatureFlag
{
Expand All @@ -28,6 +30,8 @@ public class GoFeatureFlagProvider : FeatureProvider
private ExporterMetadata _exporterMetadata;
private HttpClient _httpClient;

private IFusionCache _cache = null;

/// <summary>
/// Constructor of the provider.
/// <param name="options">Options used while creating the provider</param>
Expand Down Expand Up @@ -87,6 +91,27 @@ private void InitializeProvider(GoFeatureFlagProviderOptions options)
if (options.ApiKey != null)
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", options.ApiKey);


var cacheMaxSize = options.CacheMaxSize ?? 10000;
var cacheMaxTTL = options.CacheMaxTTL ?? TimeSpan.FromSeconds(60);

var backingMemoryCache = new MemoryCache(new MemoryCacheOptions
{
SizeLimit = cacheMaxSize,
CompactionPercentage = 0.1,
});

_cache = new FusionCache(new FusionCacheOptions
{
DefaultEntryOptions = new FusionCacheEntryOptions
{
Size = 1,
Duration = cacheMaxTTL,
},

}, backingMemoryCache);

}

/// <summary>
Expand Down Expand Up @@ -114,9 +139,13 @@ public override async Task<ResolutionDetails<bool>> ResolveBooleanValueAsync(str
{
try
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
return new ResolutionDetails<bool>(flagKey, bool.Parse(resp.Value.ToString()), ErrorType.None,
resp.Reason, resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
var key = GenerateCacheKey(flagKey, context);
return await _cache.GetOrSetAsync<ResolutionDetails<bool>>(key, async (_, _) =>
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
return new ResolutionDetails<bool>(flagKey, bool.Parse(resp.Value.ToString()), ErrorType.None,
resp.Reason, resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
}).ConfigureAwait(false);
}
catch (FormatException e)
{
Expand Down Expand Up @@ -145,12 +174,16 @@ public override async Task<ResolutionDetails<string>> ResolveStringValueAsync(st
EvaluationContext context = null, CancellationToken cancellationToken = default)
{
try
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
if (!(resp.Value is JsonElement element && element.ValueKind == JsonValueKind.String))
throw new TypeMismatchError($"flag value {flagKey} had unexpected type");
return new ResolutionDetails<string>(flagKey, resp.Value.ToString(), ErrorType.None, resp.Reason,
resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
{
var key = GenerateCacheKey(flagKey, context);
return await _cache.GetOrSetAsync<ResolutionDetails<string>>(key, async (_,_) =>
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
if (!(resp.Value is JsonElement element && element.ValueKind == JsonValueKind.String))
throw new TypeMismatchError($"flag value {flagKey} had unexpected type");
return new ResolutionDetails<string>(flagKey, resp.Value.ToString(), ErrorType.None, resp.Reason,
resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
}).ConfigureAwait(false);
}
catch (FormatException e)
{
Expand Down Expand Up @@ -179,9 +212,13 @@ public override async Task<ResolutionDetails<int>> ResolveIntegerValueAsync(stri
{
try
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
return new ResolutionDetails<int>(flagKey, int.Parse(resp.Value.ToString()), ErrorType.None,
resp.Reason, resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
var key = GenerateCacheKey(flagKey, context);
return await _cache.GetOrSetAsync<ResolutionDetails<int>>(key, async (_,_) =>
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
return new ResolutionDetails<int>(flagKey, int.Parse(resp.Value.ToString()), ErrorType.None,
resp.Reason, resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
}).ConfigureAwait(false);
}
catch (FormatException e)
{
Expand Down Expand Up @@ -211,10 +248,14 @@ public override async Task<ResolutionDetails<double>> ResolveDoubleValueAsync(st
{
try
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
return new ResolutionDetails<double>(flagKey,
double.Parse(resp.Value.ToString(), CultureInfo.InvariantCulture), ErrorType.None,
resp.Reason, resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
var key = GenerateCacheKey(flagKey, context);
return await _cache.GetOrSetAsync<ResolutionDetails<double>>(key, async (_,_) =>
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
return new ResolutionDetails<double>(flagKey,
double.Parse(resp.Value.ToString(), CultureInfo.InvariantCulture), ErrorType.None,
resp.Reason, resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
}).ConfigureAwait(false);
}
catch (FormatException e)
{
Expand Down Expand Up @@ -244,15 +285,19 @@ public override async Task<ResolutionDetails<Value>> ResolveStructureValueAsync(
{
try
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
if (resp.Value is JsonElement)
{
var value = ConvertValue((JsonElement)resp.Value);
return new ResolutionDetails<Value>(flagKey, value, ErrorType.None, resp.Reason,
resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
}

throw new TypeMismatchError($"flag value {flagKey} had unexpected type");
var key = GenerateCacheKey(flagKey, context);
return await _cache.GetOrSetAsync<ResolutionDetails<Value>>(key, async (_,_) =>
{
var resp = await CallApi(flagKey, defaultValue, context).ConfigureAwait(false);
if (resp.Value is JsonElement)
{
var value = ConvertValue((JsonElement)resp.Value);
return new ResolutionDetails<Value>(flagKey, value, ErrorType.None, resp.Reason,
resp.Variant, resp.ErrorDetails, resp.Metadata.ToImmutableMetadata());
}

throw new TypeMismatchError($"flag value {flagKey} had unexpected type");
}).ConfigureAwait(false);
}
catch (FormatException e)
{
Expand Down Expand Up @@ -311,6 +356,11 @@ private async Task<OfrepResponse> CallApi<T>(string flagKey, T defaultValue,
return ofrepResp;
}

private string GenerateCacheKey(string flagKey, EvaluationContext ctx)
{
return ctx != null ? new OfrepRequest(ctx).AsJsonString() : flagKey;
}

/// <summary>
/// convertValue is converting the object return by the proxy response in the right type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ public class GoFeatureFlagProviderOptions
/// (optional) ExporterMetadata are static information you can set that will be available in the
/// evaluation data sent to the exporter.
/// </summary>
public ExporterMetadata ExporterMetadata { get; set; }
public ExporterMetadata ExporterMetadata { get; set; }


/// <summary>
/// (optional) How long is an entry allowed to be cached in memory
/// Default: 60 seconds
/// </summary>
public TimeSpan? CacheMaxTTL { get; set; }

/// <summary>
/// (optional) How many entries may be cached
/// Default: 10000
/// </summary>
public int? CacheMaxSize { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>OpenFeature.Contrib.GOFeatureFlag</PackageId>
Expand All @@ -15,8 +15,12 @@
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.2.0" />
</ItemGroup>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another suggestion, please lower the required version of this dependency. The reason is, if a consumer is using 2.1, if they want to upgrade GoFF, they would need to upgrade to 2.2. My advise is to keep as lower as possible so no consumer is forced to upgrade. Other possible solution is to use PrivateAssets but I'm not that familiar with the full consequences.


<PropertyGroup>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

</Project>
Loading