From 7ce2bab900b71e5e9fb514ae18ba63e6eff4e808 Mon Sep 17 00:00:00 2001 From: "Ash.Wani" <149169001+wani-guanxi@users.noreply.github.com> Date: Tue, 21 Jan 2025 18:38:39 +0000 Subject: [PATCH] fix: AppConfig through exception if calling too frequently, use whats in cache --- .../AppConfigRetrievalApi.cs | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/OpenFeature.Contrib.Providers.AwsAppConfig/AppConfigRetrievalApi.cs b/src/OpenFeature.Contrib.Providers.AwsAppConfig/AppConfigRetrievalApi.cs index 10ec3c32..2e285d15 100644 --- a/src/OpenFeature.Contrib.Providers.AwsAppConfig/AppConfigRetrievalApi.cs +++ b/src/OpenFeature.Contrib.Providers.AwsAppConfig/AppConfigRetrievalApi.cs @@ -95,15 +95,27 @@ public async TaskGetLatestConfigurationAsync(Fea ConfigurationToken = await GetSessionToken(profile) }; - var response = await _appConfigDataClient.GetLatestConfigurationAsync(configurationRequest); + GetLatestConfigurationResponse response; - // If not NextPollConfigurationToken, something wrong with AWS connection. - if(string.IsNullOrWhiteSpace(response.NextPollConfigurationToken)) throw new Exception("Unable to connect to AWS"); + try + { + response = await _appConfigDataClient.GetLatestConfigurationAsync(configurationRequest); + } + catch + { + // On exception, could be because of connection issue or + // too frequent call per defined by polling duration, get what's in cache + response = null; + } - // First, update the session token to the newly returned token - _memoryCache.Set(sessionKey, response.NextPollConfigurationToken); + // Update Next Poll configuration token only when one is available. + if(response != null) + { + // First, update the session token to the newly returned token + _memoryCache.Set(sessionKey, response.NextPollConfigurationToken); + } - if((response.Configuration == null || response.Configuration.Length == 0) + if((response?.Configuration == null || response.Configuration.Length == 0) && _memoryCache.TryGetValue(configKey, out GetLatestConfigurationResponse configValue)) { // AppConfig returns empty Configuration if value hasn't changed from last retrieval, hence use what's in cache.