Sha256: 33a0ec9fc26cd83cc225a8e9ea01f5f41d71d718de55c2c5ea24a4b55d9035d2

Contents?: true

Size: 753 Bytes

Versions: 4

Compression:

Stored size: 753 Bytes

Contents

module LinodeAPI
  ##
  # A wrapper for the Raw API that can retry retryable errors
  class Retryable < Raw
    def initialize(params = {})
      super
      @options[:max_retries] ||= 3
      @options[:max_delay] ||= 60
    end

    private

    alias single_call call

    def call(*args)
      call_with_retries(max_retries, *args)
    end

    def call_with_retries(remaining, *args)
      single_call(*args)
    rescue RetryableHTTPError => e
      raise(RetriedHTTPError.new(e.code, max_retries)) if remaining < 0
      delay = [e.delay, max_delay].min
      sleep delay
      call_with_retries(remaining - 1, *args)
    end

    def max_retries
      @options[:max_retries]
    end

    def max_delay
      @options[:max_delay]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
linodeapi-2.0.3 lib/linodeapi/retryable.rb
linodeapi-2.0.2 lib/linodeapi/retryable.rb
linodeapi-2.0.1 lib/linodeapi/retryable.rb
linodeapi-2.0.0 lib/linodeapi/retryable.rb