Skip to content

Commit

Permalink
fix: Improve remote evaluation fetch retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Jan 23, 2024
1 parent 7e84f08 commit 55820a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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(message, status_code)
super(message)
@status_code = status_code
end
end
end
13 changes: 11 additions & 2 deletions lib/experiment/remote/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ def fetch_internal(user)
do_fetch(user, @config.fetch_timeout_millis)
rescue StandardError => e
@logger.error("[Experiment] Fetch failed: #{e.message}")
raise e unless should_retry_fetch?(e)

begin
return retry_fetch(user)
retry_fetch(user)
rescue StandardError => err
@logger.error("[Experiment] Retry Fetch failed: #{err.message}")
end
throw e
end

# @param [User] user
Expand Down Expand Up @@ -108,6 +109,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("Fetch error response: status=#{response.code} #{response.message}", response.code.to_i) 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 +143,11 @@ def add_context(user)
user.library = "experiment-ruby-server/#{VERSION}"
user
end

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

true
end
end
end

0 comments on commit 55820a5

Please sign in to comment.