Skip to content

Commit

Permalink
Fix initial config load when auto poll enabled with results from cache (
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-cat authored May 22, 2024
1 parent 3c2d2bb commit 29555eb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions configcatclient/configservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def __init__(self, sdk_key, polling_mode, hooks, config_fetcher, log, config_cac
self._set_initialized()

def get_config(self):
threshold = utils.distant_past
prefer_cached = self._initialized.is_set()
if isinstance(self._polling_mode, LazyLoadingMode):
entry, _ = self._fetch_if_older(
utils.get_utc_now_seconds_since_epoch() - self._polling_mode.cache_refresh_interval_seconds)
return (entry.config, entry.fetch_time) \
if not entry.is_empty() \
else (None, utils.distant_past)
threshold = utils.get_utc_now_seconds_since_epoch() - self._polling_mode.cache_refresh_interval_seconds
prefer_cached = False
elif isinstance(self._polling_mode, AutoPollingMode) and not self._initialized.is_set():
elapsed_time = (utils.get_utc_now() - self._start_time).total_seconds()
threshold = utils.get_utc_now_seconds_since_epoch() - self._polling_mode.poll_interval_seconds
if elapsed_time < self._polling_mode.max_init_wait_time_seconds:
self._initialized.wait(self._polling_mode.max_init_wait_time_seconds - elapsed_time)

Expand All @@ -51,7 +51,7 @@ def get_config(self):
else (None, utils.distant_past)

# If we are initialized, we prefer the cached results
entry, _ = self._fetch_if_older(utils.distant_past, prefer_cache=self._initialized.is_set())
entry, _ = self._fetch_if_older(threshold, prefer_cached=prefer_cached)
return (entry.config, entry.fetch_time) \
if not entry.is_empty() \
else (None, utils.distant_past)
Expand Down Expand Up @@ -98,7 +98,7 @@ def close(self):
if isinstance(self._polling_mode, AutoPollingMode):
self._stopped.set()

def _fetch_if_older(self, threshold, prefer_cache=False):
def _fetch_if_older(self, threshold, prefer_cached=False):
"""
:return: Returns the ConfigEntry object and error message in case of any error.
"""
Expand All @@ -116,7 +116,7 @@ def _fetch_if_older(self, threshold, prefer_cache=False):
return self._cached_entry, None

# If we are in offline mode or the caller prefers cached values, do not initiate fetch.
if self._is_offline or prefer_cache:
if self._is_offline or prefer_cached:
return self._cached_entry, None

# No fetch is running, initiate a new one.
Expand Down
2 changes: 1 addition & 1 deletion configcatclient/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONFIGCATCLIENT_VERSION = "9.0.3"
CONFIGCATCLIENT_VERSION = "9.0.4"
2 changes: 1 addition & 1 deletion configcatclienttests/test_rollout.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def test_attribute_conversion_to_canonical_string(self, key, custom_attribute_va
])
def test_comparison_attribute_trimming(self, key, expected_return_value):
config = LocalFileDataSource(path.join(self.script_dir, 'data/comparison_attribute_trimming.json'),
OverrideBehaviour.LocalOnly, None).get_overrides()
OverrideBehaviour.LocalOnly, None).get_overrides()

log = Logger('configcat', Hooks())
logger = logging.getLogger('configcat')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def parse_requirements(filename):
return [line for line in lines if line]


configcatclient_version = '9.0.3'
configcatclient_version = '9.0.4'

requirements = parse_requirements('requirements.txt')

Expand Down

0 comments on commit 29555eb

Please sign in to comment.