Sha256: de8ff79834217159ae30477394a0c7661056523e5aa099582967b406687f70ac

Contents?: true

Size: 765 Bytes

Versions: 5

Compression:

Stored size: 765 Bytes

Contents

module SmartyStreets
  class RetrySender
    MAX_BACKOFF_DURATION = 10
    STATUS_OK = '200'.freeze

    def initialize(max_retries, inner, sleeper, logger)
      @max_retries = max_retries
      @inner = inner
      @sleeper = sleeper
      @logger = logger
    end

    def send(request)
      response = @inner.send(request)

      (0..@max_retries-1).each do |i|
        break if response.status_code == STATUS_OK

        backoff(i)

        response = @inner.send(request)
      end

      response
    end

    def backoff(attempt)
      backoff_duration = [attempt, MAX_BACKOFF_DURATION].min

      @logger.log("There was an error processing the request. Retrying in #{backoff_duration} seconds...")
      @sleeper.sleep(backoff_duration)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
smartystreets_ruby_sdk-5.0.0 lib/smartystreets_ruby_sdk/retry_sender.rb
smartystreets_ruby_sdk-4.2.0 lib/smartystreets_ruby_sdk/retry_sender.rb
smartystreets_ruby_sdk-4.1.3 lib/smartystreets_ruby_sdk/retry_sender.rb
smartystreets_ruby_sdk-4.0.1 lib/smartystreets_ruby_sdk/retry_sender.rb
smartystreets_ruby_sdk-4.0.0 lib/smartystreets_ruby_sdk/retry_sender.rb