lib/aptible/resource/default_retry_coordinator.rb in aptible-resource-0.3.8 vs lib/aptible/resource/default_retry_coordinator.rb in aptible-resource-0.4.0

- old
+ new

@@ -1,15 +1,37 @@ module Aptible module Resource class DefaultRetryCoordinator - attr_reader :resource + 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?(_error) - false + 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