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

fix: Improve remote evaluation fetch retry logic #59

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions lib/amplitude-experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'experiment/local/assignment/assignment_config'
require 'experiment/util/lru_cache'
require 'experiment/util/hash'
require 'experiment/error'

# Amplitude Experiment Module
module AmplitudeExperiment
Expand Down
11 changes: 11 additions & 0 deletions lib/experiment/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module AmplitudeExperiment
# FetchError
class FetchError < StandardError
attr_reader :status_code

def initialize(status_code, message)
super(message)
@status_code = status_code
end
end
end
20 changes: 15 additions & 5 deletions lib/experiment/remote/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ def fetch_internal(user)
do_fetch(user, @config.fetch_timeout_millis)
rescue StandardError => e
@logger.error("[Experiment] Fetch failed: #{e.message}")
begin
return retry_fetch(user)
rescue StandardError => err
@logger.error("[Experiment] Retry Fetch failed: #{err.message}")
if should_retry_fetch?(e)
begin
retry_fetch(user)
rescue StandardError => err
@logger.error("[Experiment] Retry Fetch failed: #{err.message}")
end
end
throw e
raise e
tyiuhc marked this conversation as resolved.
Show resolved Hide resolved
end

# @param [User] user
Expand Down Expand Up @@ -108,6 +110,8 @@ def do_fetch(user, timeout_millis)
end_time = Time.now
elapsed = (end_time - start_time) * 1000.0
@logger.debug("[Experiment] Fetch complete in #{elapsed.round(3)} ms")
raise FetchError.new(response.code.to_i, "Fetch error response: status=#{response.code} #{response.message}") if response.code != '200'

json = JSON.parse(response.body)
variants = parse_json_variants(json)
@logger.debug("[Experiment] Fetched variants: #{variants}")
Expand Down Expand Up @@ -140,5 +144,11 @@ def add_context(user)
user.library = "experiment-ruby-server/#{VERSION}"
user
end

def should_retry_fetch?(err)
return err.status_code < 400 || err.status_code >= 500 || err.status_code == 429 if err.is_a?(FetchError)

true
end
end
end