Sha256: d470fedd362e95decdc42d1bce88c6c636df917bd21fc6651833aadcc8f654ab

Contents?: true

Size: 939 Bytes

Versions: 5

Compression:

Stored size: 939 Bytes

Contents

module Aptible
  module Resource
    class DefaultRetryCoordinator
      attr_reader :resource, :retry_schedule

      IDEMPOTENT_METHODS = [
        # Idempotent as per RFC
        :delete, :get, :head, :options, :put,
        # Idempotent on our APIs
        :patch
      ].freeze
      RETRY_ERRORS = [Faraday::Error, HyperResource::ServerError].freeze

      def initialize(resource)
        @resource = resource
        @retry_schedule = new_retry_schedule
      end

      def retry?(method, err)
        # rubocop:disable Style/CaseEquality
        return false unless RETRY_ERRORS.any? { |c| c === err }
        return false unless IDEMPOTENT_METHODS.include?(method)
        retry_in = retry_schedule.shift
        return false if retry_in.nil?
        sleep retry_in
        true
        # rubocop:enable Style/CaseEquality
      end

      private

      def new_retry_schedule
        [0.2, 0.8, 2]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
aptible-resource-1.0.2 lib/aptible/resource/default_retry_coordinator.rb
aptible-resource-1.0.1 lib/aptible/resource/default_retry_coordinator.rb
aptible-resource-1.0.0 lib/aptible/resource/default_retry_coordinator.rb
aptible-resource-0.5.0 lib/aptible/resource/default_retry_coordinator.rb
aptible-resource-0.4.0 lib/aptible/resource/default_retry_coordinator.rb