From b96558cfe95f0638187946987f7a6f18b97aacff Mon Sep 17 00:00:00 2001 From: Chris Hannon Date: Thu, 25 Jan 2024 11:56:01 -0700 Subject: [PATCH] Various PR fixes --- .../ApiTests/FeatureRefreshWorkerTests.cs | 2 +- GrowthBook/Api/FeatureRefreshWorker.cs | 4 ++-- GrowthBook/Api/HttpHeaders.cs | 15 +++++++++++++++ GrowthBook/Api/InMemoryFeatureCache.cs | 4 +++- GrowthBook/Context.cs | 9 ++------- GrowthBook/Experiment.cs | 2 +- GrowthBook/GrowthBook.cs | 13 ++++++------- GrowthBook/GrowthBook.csproj | 2 -- 8 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 GrowthBook/Api/HttpHeaders.cs diff --git a/GrowthBook.Tests/ApiTests/FeatureRefreshWorkerTests.cs b/GrowthBook.Tests/ApiTests/FeatureRefreshWorkerTests.cs index b3b24e1..8f521f3 100644 --- a/GrowthBook.Tests/ApiTests/FeatureRefreshWorkerTests.cs +++ b/GrowthBook.Tests/ApiTests/FeatureRefreshWorkerTests.cs @@ -58,7 +58,7 @@ public TestDelegatingHandler(HttpStatusCode statusCode, string jsonContent, stri // Indicate in the HTTP response that the server sent events are supported // in order to allow kicking off the background listener. - response.Headers.Add("x-sse-support", "enabled"); + response.Headers.Add(HttpHeaders.ServerSentEvents.Key, HttpHeaders.ServerSentEvents.EnabledValue); } handleCount++; diff --git a/GrowthBook/Api/FeatureRefreshWorker.cs b/GrowthBook/Api/FeatureRefreshWorker.cs index c8253a5..b9e3154 100644 --- a/GrowthBook/Api/FeatureRefreshWorker.cs +++ b/GrowthBook/Api/FeatureRefreshWorker.cs @@ -84,7 +84,7 @@ public async Task> RefreshCacheFromApi(Cancellation if (_config.PreferServerSentEvents) { - _isServerSentEventsEnabled = response.Headers.TryGetValues("x-sse-support", out var values) && values.Contains("enabled"); + _isServerSentEventsEnabled = response.Headers.TryGetValues(HttpHeaders.ServerSentEvents.Key, out var values) && values.Contains(HttpHeaders.ServerSentEvents.EnabledValue); _logger.LogDebug($"{nameof(FeatureRefreshWorker)} is configured to prefer server sent events and enabled is now '{_isServerSentEventsEnabled}'"); EnsureCorrectRefreshModeIsActive(); @@ -127,7 +127,7 @@ private Task ListenForServerSentEvents() try { _logger.LogInformation($"Making an HTTP request to server sent events endpoint '{_serverSentEventsApiEndpoint}'"); - + var httpClient = _httpClientFactory.CreateClient(ConfiguredClients.ServerSentEventsApiClient); var stream = await httpClient.GetStreamAsync(_serverSentEventsApiEndpoint); diff --git a/GrowthBook/Api/HttpHeaders.cs b/GrowthBook/Api/HttpHeaders.cs new file mode 100644 index 0000000..25693c0 --- /dev/null +++ b/GrowthBook/Api/HttpHeaders.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GrowthBook.Api +{ + public static class HttpHeaders + { + public static class ServerSentEvents + { + public const string Key = "x-sse-support"; + public const string EnabledValue = "enabled"; + } + } +} diff --git a/GrowthBook/Api/InMemoryFeatureCache.cs b/GrowthBook/Api/InMemoryFeatureCache.cs index 094dbe8..a0d2394 100644 --- a/GrowthBook/Api/InMemoryFeatureCache.cs +++ b/GrowthBook/Api/InMemoryFeatureCache.cs @@ -65,12 +65,14 @@ public Task> GetFeatures(CancellationToken? cancell } } - public async Task RefreshWith(IDictionary features, CancellationToken? cancellationToken = null) + public Task RefreshWith(IDictionary features, CancellationToken? cancellationToken = null) { lock(_cacheLock) { _cachedFeatures = new Dictionary(features); _nextCacheExpiration = DateTime.UtcNow.AddSeconds(_cacheExpirationInSeconds); + + return Task.CompletedTask; } } } diff --git a/GrowthBook/Context.cs b/GrowthBook/Context.cs index 1c89b36..a25a8f0 100644 --- a/GrowthBook/Context.cs +++ b/GrowthBook/Context.cs @@ -76,13 +76,8 @@ public class Context public IGrowthBookFeatureRepository FeatureRepository { get; set; } /// - /// A logger factory implementation that will override the default GrowthBook implementation. Optional. + /// A logger factory implementation that will enable logging throughout the SDK. Optional. /// - public ILoggerFactory DefaultLoggerFactory { get; set; } - - /// - /// The default log level for the default GrowthBook logging implementation. Optional. - /// - public LogLevel DefaultLogLevel { get; set; } = LogLevel.Warning; + public ILoggerFactory LoggerFactory { get; set; } } } diff --git a/GrowthBook/Experiment.cs b/GrowthBook/Experiment.cs index ab67019..3be6772 100644 --- a/GrowthBook/Experiment.cs +++ b/GrowthBook/Experiment.cs @@ -26,7 +26,7 @@ public class Experiment /// /// How to weight traffic between variations. Must add to 1. /// - public List Weights { get; set; } + public IList Weights { get; set; } /// /// If set to false, always return the control (first variation). diff --git a/GrowthBook/GrowthBook.cs b/GrowthBook/GrowthBook.cs index 885aab0..48afa24 100644 --- a/GrowthBook/GrowthBook.cs +++ b/GrowthBook/GrowthBook.cs @@ -62,13 +62,11 @@ public GrowthBook(Context context) var featureCache = new InMemoryFeatureCache(cacheExpirationInSeconds: 60); var httpClientFactory = new HttpClientFactory(requestTimeoutInSeconds: 60); - var loggerFactory = context.DefaultLoggerFactory ?? LoggerFactory.Create(builder => - { - builder - .SetMinimumLevel(context.DefaultLogLevel) - .AddConsole() - .AddDebug(); - }); + // If they didn't want to include a logger factory, just create a basic one that will + // create disabled loggers by default so we don't force a particular logging provider + // or logs on the user if they chose the defaults. + + var loggerFactory = context.LoggerFactory ?? LoggerFactory.Create(builder => { }); var conditionEvaluatorLogger = loggerFactory.CreateLogger(); var featureRefreshLogger = loggerFactory.CreateLogger(); @@ -307,6 +305,7 @@ public ExperimentResult Run(Experiment experiment) } } + /// public async Task LoadFeatures(GrowthBookRetrievalOptions options = null, CancellationToken? cancellationToken = null) { try diff --git a/GrowthBook/GrowthBook.csproj b/GrowthBook/GrowthBook.csproj index 4e18218..e53a616 100644 --- a/GrowthBook/GrowthBook.csproj +++ b/GrowthBook/GrowthBook.csproj @@ -19,8 +19,6 @@ - -