Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use last modified headers #108

Merged
merged 9 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .github/dependabot.yml

This file was deleted.

26 changes: 20 additions & 6 deletions lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'concurrent-ruby'
require 'typhoeus'
require 'json'
require 'time'

module DevCycle
class ConfigManager
Expand All @@ -19,6 +20,7 @@ def initialize(sdkKey, local_bucketing, wait_for_init)
@local_bucketing = local_bucketing
@sdkKey = sdkKey
@config_e_tag = ""
@config_last_modified = ""
@logger = local_bucketing.options.logger
@polling_enabled = true
@max_config_retries = 2
Expand Down Expand Up @@ -53,22 +55,33 @@ def fetch_config
Accept: "application/json",
})

if @config_last_modified != ""
req.options[:headers]["If-Modified-Since"] = Time.httpdate(@config_last_modified)
end

if @config_e_tag != ""
req.options[:headers]['If-None-Match'] = @config_e_tag
end

@max_config_retries.times do
@logger.debug("Requesting new config from #{get_config_url}, current etag: #{@config_e_tag}")
@logger.debug("Requesting new config from #{get_config_url}, current etag: #{@config_e_tag}, last modified: #{@config_last_modified}")
resp = req.run
@logger.debug("Config request complete, status: #{resp.code}")
case resp.code
when 304
@logger.debug("Config not modified, using cache, etag: #{@config_e_tag}")
@logger.debug("Config not modified, using cache, etag: #{@config_e_tag}, last modified: #{@config_last_modified}")
break
when 200
@logger.debug("New config received, etag: #{resp.headers['Etag']}")
set_config(resp.body, resp.headers['Etag'])
@logger.debug("New config stored, etag: #{@config_e_tag}")
@logger.debug("New config received, etag: #{resp.headers['Etag']} LM:#{resp.headers['Last-Modified']}")
lm_header = resp.headers['Last-Modified']
lm_timestamp = Time.rfc2822(lm_header)
current_lm = Time.rfc2822(@config_last_modified)
if current_lm.utc < lm_timestamp.utc
set_config(resp.body, resp.headers['Etag'], lm_header)
@logger.debug("New config stored, etag: #{@config_e_tag}, last modified: #{lm_header}")
else
@logger.warn("Config response was an older config than currently stored config.")
end
break
when 403
stop_polling
Expand All @@ -91,13 +104,14 @@ def fetch_config
nil
end

def set_config(config, etag)
def set_config(config, etag, lastmodified)
if !JSON.parse(config).is_a?(Hash)
raise("Invalid JSON body parsed from Config Response")
end

@local_bucketing.store_config(config)
@config_e_tag = etag
@config_last_modified = lastmodified
@local_bucketing.has_config = true
end

Expand Down
Loading