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 d32ca1e commit f51153b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lib/configcat/configservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ def initialize(sdk_key, polling_mode, hooks, config_fetcher, log, config_cache,
end

def get_config
threshold = Utils::DISTANT_PAST
prefer_cached = @initialized.set?
if @polling_mode.is_a?(LazyLoadingMode)
entry, _ = fetch_if_older(Utils.get_utc_now_seconds_since_epoch - @polling_mode.cache_refresh_interval_seconds)
return !entry.empty? ?
[entry.config, entry.fetch_time] :
[nil, Utils::DISTANT_PAST]
threshold = Utils.get_utc_now_seconds_since_epoch - @polling_mode.cache_refresh_interval_seconds
prefer_cached = false
elsif @polling_mode.is_a?(AutoPollingMode) && !@initialized.set?
elapsed_time = Utils.get_utc_now_seconds_since_epoch - @start_time # Elapsed time in seconds
threshold = Utils.get_utc_now_seconds_since_epoch - @polling_mode.poll_interval_seconds
if elapsed_time < @polling_mode.max_init_wait_time_seconds
@initialized.wait(@polling_mode.max_init_wait_time_seconds - elapsed_time)

Expand All @@ -52,7 +53,7 @@ def get_config
end

# If we are initialized, we prefer the cached results
entry, _ = fetch_if_older(Utils::DISTANT_PAST, prefer_cache: @initialized.set?)
entry, _ = fetch_if_older(threshold, prefer_cached: prefer_cached)
return !entry.empty? ?
[entry.config, entry.fetch_time] :
[nil, Utils::DISTANT_PAST]
Expand Down Expand Up @@ -117,7 +118,7 @@ def self.get_cache_key(sdk_key)
end

# :return [ConfigEntry, String] Returns the ConfigEntry object and error message in case of any error.
def fetch_if_older(threshold, prefer_cache: false)
def fetch_if_older(threshold, prefer_cached: false)
# Sync up with the cache and use it when it's not expired.
@lock.synchronize do
# Sync up with the cache and use it when it's not expired.
Expand All @@ -134,7 +135,7 @@ def fetch_if_older(threshold, prefer_cache: false)
end

# If we are in offline mode or the caller prefers cached values, do not initiate fetch.
if @is_offline || prefer_cache
if @is_offline || prefer_cached
return @cached_entry, nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/configcat/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ConfigCat
VERSION = "8.0.0"
VERSION = "8.0.1"
end

0 comments on commit f51153b

Please sign in to comment.