Skip to content

Commit

Permalink
Add configurable retry on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadicoder committed Sep 25, 2024
1 parent 710ff4c commit 88fae28
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Primo.configure do |config|
# By default timeout is set to 5 seconds
config.timeout = 10
# By default retries is set to 3 times
config.retries = 3
# By default we validate parameters.
config.validate_parameters = false
Expand Down
3 changes: 2 additions & 1 deletion lib/primo/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Configuration
attr_accessor :apikey, :region, :operator, :field, :precision
attr_accessor :context, :environment, :inst, :vid, :scope, :pcavailability, :enable_loggable
attr_accessor :timeout, :validate_parameters, :enable_log_requests, :enable_debug_output
attr_accessor :logger, :log_level, :log_format, :debug_output_stream
attr_accessor :logger, :log_level, :log_format, :debug_output_stream, :retries

def initialize
@apikey = "TEST_API_KEY"
Expand All @@ -52,6 +52,7 @@ def initialize
@pcavailability = false
@enable_loggable = false
@timeout = 5
@retries = 3
@validate_parameters = true
@enable_log_requests = false
# debug_output should only be enabled in development mode.
Expand Down
11 changes: 10 additions & 1 deletion lib/primo/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ def self.get(params = {})
params = params.to_h.transform_keys(&:to_sym)
method = get_method params
(url, query) = url(params)
new super(url, query:, timeout: Primo.configuration.timeout(params)), method
retry_count = Primo.configuration.retries
begin
new super(url, query:, timeout: Primo.configuration.timeout(params)), method
rescue Net::ReadTimeout
if (retry_count -= 1) > 0
retry
else
raise "Timeout retry limit exceeded"
end
end
end

def self.url(params = {})
Expand Down
1 change: 1 addition & 0 deletions spec/lib/primo/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
expect(Primo.configuration.scope).to eql nil
expect(Primo.configuration.pcavailability).to eql false
expect(Primo.configuration.timeout).to eql 5
expect(Primo.configuration.retries).to eql 3
expect(Primo.configuration.validate_parameters).to eql true
expect(Primo.configuration.enable_log_requests).to eql false
expect(Primo.configuration.logger).to be_instance_of(Logger)
Expand Down

0 comments on commit 88fae28

Please sign in to comment.