@@ -45,15 +45,16 @@ ConfigService::~ConfigService() {
4545}
4646
4747SettingResult ConfigService::getSettings () {
48+ auto threshold = kDistantPast ;
49+ auto preferCached = initialized;
4850 if (pollingMode->getPollingIdentifier () == LazyLoadingMode::kIdentifier ) {
4951 auto & lazyPollingMode = (LazyLoadingMode&)*pollingMode;
50- auto now = chrono::steady_clock::now ();
51- auto [ entry, _0, _1 ] = fetchIfOlder (get_utcnowseconds_since_epoch () - lazyPollingMode.cacheRefreshIntervalInSeconds );
52- auto config = cachedEntry->config ;
53- return { (cachedEntry != ConfigEntry::empty && config) ? config->getSettingsOrEmpty () : nullptr , entry->fetchTime };
52+ threshold = get_utcnowseconds_since_epoch () - lazyPollingMode.cacheRefreshIntervalInSeconds ;
53+ preferCached = false ;
5454 } else if (pollingMode->getPollingIdentifier () == AutoPollingMode::kIdentifier && !initialized) {
5555 auto & autoPollingMode = (AutoPollingMode&)*pollingMode;
5656 auto elapsedTime = chrono::duration<double >(chrono::steady_clock::now () - startTime).count ();
57+ threshold = get_utcnowseconds_since_epoch () - autoPollingMode.autoPollIntervalInSeconds ;
5758 if (elapsedTime < autoPollingMode.maxInitWaitTimeInSeconds ) {
5859 unique_lock<mutex> lock (initMutex);
5960 chrono::duration<double > timeout (autoPollingMode.maxInitWaitTimeInSeconds - elapsedTime);
@@ -69,7 +70,7 @@ SettingResult ConfigService::getSettings() {
6970 }
7071
7172 // If we are initialized, we prefer the cached results
72- auto [ entry, _0, _1 ] = fetchIfOlder (kDistantPast , initialized );
73+ auto [ entry, _0, _1 ] = fetchIfOlder (threshold, preferCached );
7374 auto config = entry->config ;
7475 return { (cachedEntry != ConfigEntry::empty && config) ? config->getSettingsOrEmpty () : nullptr , entry->fetchTime };
7576}
@@ -120,7 +121,7 @@ void ConfigService::setOffline() {
120121string ConfigService::generateCacheKey (const string& sdkKey) {
121122 return SHA1 ()(sdkKey + " _" + ConfigFetcher::kConfigJsonName + " _" + ConfigEntry::kSerializationFormatVersion );
122123}
123- tuple<shared_ptr<const ConfigEntry>, std::optional<std::string>, std::exception_ptr> ConfigService::fetchIfOlder (double threshold, bool preferCache ) {
124+ tuple<shared_ptr<const ConfigEntry>, std::optional<std::string>, std::exception_ptr> ConfigService::fetchIfOlder (double threshold, bool preferCached ) {
124125 {
125126 lock_guard<mutex> lock (fetchMutex);
126127
@@ -138,7 +139,7 @@ tuple<shared_ptr<const ConfigEntry>, std::optional<std::string>, std::exception_
138139 }
139140
140141 // If we are in offline mode or the caller prefers cached values, do not initiate fetch.
141- if (offline || preferCache ) {
142+ if (offline || preferCached ) {
142143 return { cachedEntry, nullopt , nullptr };
143144 }
144145
0 commit comments