dnsmadeeasy-rest-api icon indicating copy to clipboard operation
dnsmadeeasy-rest-api copied to clipboard

Implement retries for API calls

Open abriel opened this issue 10 years ago • 3 comments

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.

abriel avatar Sep 29 '15 20:09 abriel

Please do not hide errors from clients.

webervin avatar Oct 26 '16 10:10 webervin

@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 avatar Nov 30 '17 05:11 kigster

@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.

abriel avatar Nov 30 '17 10:11 abriel