Skip to content

Commit

Permalink
FB-272: Lazily acquire FeatureBoardStateSnapshot to fix(?) startup ra…
Browse files Browse the repository at this point in the history
…ce condition
  • Loading branch information
ickers committed Jan 12, 2024
1 parent f475ddc commit 7394714
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
1 change: 1 addition & 0 deletions libs/dotnet-sdk-test/FeatureBoardClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class FeatureBoardClientTests
public FeatureBoardClientTests()
{
Services.AddTransient(typeof(ILogger<>), typeof(NullLogger<>));
Services.AddTransient(typeof(Lazy<>));
Services.AddTransient<FeatureBoardClient<TestFeatures>>();

var audienceMock = Services.AddServiceMock<IAudienceProvider>((_, mock) =>
Expand Down
9 changes: 4 additions & 5 deletions libs/dotnet-sdk/FeatureBoardClient.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;
using FeatureBoard.DotnetSdk.Attributes;
using FeatureBoard.DotnetSdk.Helpers;
using FeatureBoard.DotnetSdk.Models;
using FeatureBoard.DotnetSdk.State;
using Microsoft.Extensions.Logging;

namespace FeatureBoard.DotnetSdk;

internal class FeatureBoardClient<TFeatures> : IFeatureBoardClient<TFeatures> where TFeatures : class, IFeatures
{
private readonly FeatureBoardStateSnapshot _state;
private readonly Lazy<FeatureBoardStateSnapshot> _state;
private readonly IAudienceProvider _audienceProvider;
private readonly ILogger _logger;

public FeatureBoardClient(FeatureBoardStateSnapshot state, IAudienceProvider audienceProvider, ILogger<FeatureBoardClient<TFeatures>> logger)
public FeatureBoardClient(Lazy<FeatureBoardStateSnapshot> state, IAudienceProvider audienceProvider, ILogger<FeatureBoardClient<TFeatures>> logger)
{
_state = state;
_audienceProvider = audienceProvider;
Expand Down Expand Up @@ -84,7 +83,7 @@ private static TProp GetFeatureValue<TProp>(Expression<Func<TFeatures, TProp>> e

private JsonValue? GetFeatureConfigurationValue(string featureKey, string? defaultValue)
{
var feature = _state.Get(featureKey);
var feature = _state.Value.Get(featureKey);
if (feature == null)
{
_logger.LogDebug("GetFeatureValue - no value, returning user fallback: {defaultValue}", defaultValue);
Expand Down
2 changes: 1 addition & 1 deletion libs/dotnet-sdk/Registration/RegisterFeatureBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static FeatureBoardBuilder AddFeatureBoard<TFeatures>(this IServiceCollec

services.AddSingleton<FeatureBoardState>()
.AddHostedService(static provider => provider.GetRequiredService<FeatureBoardState>())
.AddScoped(static provider => provider.GetRequiredService<FeatureBoardState>().GetSnapshot())
.AddScoped(static provider => new Lazy<FeatureBoardStateSnapshot>(provider.GetRequiredService<FeatureBoardState>().GetSnapshot))
.AddTransient<FeatureConfigurationUpdated>(static provider =>
{
var service = provider.GetRequiredService<FeatureBoardState>();
Expand Down

0 comments on commit 7394714

Please sign in to comment.