Implement retries for API calls
Hi,
During a making api calls I got JSON::ParserError. API server returns HTML contains Submit a support ticket
I think it is due to some internal error and it is temporary. After a few seconds API call finished successful.
Can we wrap low level http request into begin .. rescue retry? at least a few times with some sleep interval before the final raise.
Thanks.
Please do not hide errors from clients.
@abriel Seems like it's a very easy thing to do in the code that uses this gem.
How about this concept:
class ExceptionExceededRetries < StandardError
attr_accessor :retries
def initialize(exception, retries = nil)
super(exception)
self.retries = retries
end
end
def with_retries(tries = 3, retriable_errors = [StandardError])
yield(tries) if block_given?
rescue *retriable_errors => e
count ||= 0
if (tries -= 1) >= 0
sleep rand(0..0.1)
puts "retrying tries are at #{tries}"
count += 1
retry
else
raise(ExceptionExceededRetries.new(e, count))
end
end
begin
with_retries(3, [NameError]) do
BlahBlahBlah.new
end
rescue ExceptionExceededRetries => e
puts "retries exceeded after #{e.retries} retries: #{e.message}"
end
@kigster , you absolutely right. Instead of pushing to make this "bicycle" by many people I just suggested to have implementation in the library. Again to this thread, even AWS cannot guarantee 100% availability of its API and implemented retry policy in the lib. To handle short-term outages. Thank you @kigster for your interest here.