Skip to content

Commit d4056a0

Browse files
authored
Fix initial config load when auto poll enabled with results from cache (#27)
1 parent 5c3939d commit d4056a0

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

src/configentry.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ shared_ptr<const ConfigEntry> ConfigEntry::fromString(const string& text) {
3030
}
3131

3232
auto eTag = text.substr(fetchTimeIndex + 1, eTagIndex - fetchTimeIndex - 1);
33-
if (eTag.empty()) {
34-
throw invalid_argument("Empty eTag value");
35-
}
3633

3734
auto configJsonString = text.substr(eTagIndex + 1);
3835
try {

src/configservice.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ ConfigService::~ConfigService() {
4545
}
4646

4747
SettingResult 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() {
120121
string 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

src/configservice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ConfigService {
4040

4141
private:
4242
// Returns the ConfigEntry object and error message in case of any error.
43-
std::tuple<std::shared_ptr<const ConfigEntry>, std::optional<std::string>, std::exception_ptr> fetchIfOlder(double threshold, bool preferCache = false);
43+
std::tuple<std::shared_ptr<const ConfigEntry>, std::optional<std::string>, std::exception_ptr> fetchIfOlder(double threshold, bool preferCached = false);
4444
void setInitialized();
4545
std::shared_ptr<const ConfigEntry> readCache();
4646
void writeCache(const std::shared_ptr<const ConfigEntry>& configEntry);

src/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#pragma once
22

3-
#define CONFIGCAT_VERSION "4.0.0"
3+
#define CONFIGCAT_VERSION "4.0.1"

0 commit comments

Comments
 (0)