@@ -45,15 +45,16 @@ ConfigService::~ConfigService() {
45
45
}
46
46
47
47
SettingResult ConfigService::getSettings () {
48
+ auto threshold = kDistantPast ;
49
+ auto preferCached = initialized;
48
50
if (pollingMode->getPollingIdentifier () == LazyLoadingMode::kIdentifier ) {
49
51
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 ;
54
54
} else if (pollingMode->getPollingIdentifier () == AutoPollingMode::kIdentifier && !initialized) {
55
55
auto & autoPollingMode = (AutoPollingMode&)*pollingMode;
56
56
auto elapsedTime = chrono::duration<double >(chrono::steady_clock::now () - startTime).count ();
57
+ threshold = get_utcnowseconds_since_epoch () - autoPollingMode.autoPollIntervalInSeconds ;
57
58
if (elapsedTime < autoPollingMode.maxInitWaitTimeInSeconds ) {
58
59
unique_lock<mutex> lock (initMutex);
59
60
chrono::duration<double > timeout (autoPollingMode.maxInitWaitTimeInSeconds - elapsedTime);
@@ -69,7 +70,7 @@ SettingResult ConfigService::getSettings() {
69
70
}
70
71
71
72
// 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 );
73
74
auto config = entry->config ;
74
75
return { (cachedEntry != ConfigEntry::empty && config) ? config->getSettingsOrEmpty () : nullptr , entry->fetchTime };
75
76
}
@@ -120,7 +121,7 @@ void ConfigService::setOffline() {
120
121
string ConfigService::generateCacheKey (const string& sdkKey) {
121
122
return SHA1 ()(sdkKey + " _" + ConfigFetcher::kConfigJsonName + " _" + ConfigEntry::kSerializationFormatVersion );
122
123
}
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 ) {
124
125
{
125
126
lock_guard<mutex> lock (fetchMutex);
126
127
@@ -138,7 +139,7 @@ tuple<shared_ptr<const ConfigEntry>, std::optional<std::string>, std::exception_
138
139
}
139
140
140
141
// If we are in offline mode or the caller prefers cached values, do not initiate fetch.
141
- if (offline || preferCache ) {
142
+ if (offline || preferCached ) {
142
143
return { cachedEntry, nullopt, nullptr };
143
144
}
144
145
0 commit comments