Sha256: 81a671ebc0b2f89103180f54c0a7824a8d7eecc62c4018fb5606c6d171139b24

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

require 'smartsheet/constants'

module Smartsheet
  module API
    # Accepts a callable proc and a predicate, calling the proc repeatedly with backoff until the
    # provided time limit is reached, or until the predicate applied to the proc's result returns
    # false.
    class RetryLogic
      include Smartsheet::Constants

      def initialize(max_retry_time: DEFAULT_MAX_RETRY_TIME, backoff_method: DEFAULT_BACKOFF_METHOD)
        @max_retry_time = max_retry_time
        @backoff_method = backoff_method
      end

      def run(should_retry, &method_to_run)
        end_time = Time.now.to_i + max_retry_time

        _run(method_to_run, should_retry, end_time, 0)
      end

      private

      attr_reader :backoff_method, :max_retry_time

      def _run(method_to_run, should_retry, end_time, iteration)
        result = method_to_run.call(iteration)

        backoff = backoff_method.call(iteration, result)

        if backoff == :stop || Time.now.to_i + backoff >= end_time || !should_retry.call(result)
          return result
        end

        sleep backoff
        _run(method_to_run, should_retry, end_time, iteration + 1)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smartsheet-2.101.1 lib/smartsheet/api/retry_logic.rb
smartsheet-2.101.0 lib/smartsheet/api/retry_logic.rb
smartsheet-2.86.1 lib/smartsheet/api/retry_logic.rb
smartsheet-2.86.0 lib/smartsheet/api/retry_logic.rb
smartsheet-2.77.2 lib/smartsheet/api/retry_logic.rb
smartsheet-1.0.0 lib/smartsheet/api/retry_logic.rb