Sha256: 6d6e803d17ef65c55aa0bbbc1cd1e1437f604eb9173312fd95581c6b2c17a520

Contents?: true

Size: 1.89 KB

Versions: 19

Compression:

Stored size: 1.89 KB

Contents

require "faraday/middleware"
module ZendeskAPI
  module Middleware
    # @private
    module Request
      # Faraday middleware to handle HTTP Status 429 (rate limiting) / 503 (maintenance)
      # @private
      class Retry < Faraday::Middleware
        DEFAULT_RETRY_AFTER = 10
        DEFAULT_ERROR_CODES = [429, 503]

        def initialize(app, options = {})
          super(app)
          @logger = options[:logger]
          @error_codes = options.key?(:retry_codes) && options[:retry_codes] ? options[:retry_codes] : DEFAULT_ERROR_CODES
          @retry_on_exception = options.key?(:retry_on_exception) && options[:retry_on_exception] ? options[:retry_on_exception] : false
        end

        def call(env)
          original_env = env.dup
          exception_happened = false
          if @retry_on_exception
            begin
              response = @app.call(env)
            rescue StandardError => e
              exception_happened = true
            end
          else
            response = @app.call(env)
          end

          if exception_happened || @error_codes.include?(response.env[:status])

            if exception_happened
              seconds_left = DEFAULT_RETRY_AFTER.to_i
              @logger.warn "An exception happened, waiting #{seconds_left} seconds... #{e}" if @logger
            else
              seconds_left = (response.env[:response_headers][:retry_after] || DEFAULT_RETRY_AFTER).to_i
            end

            @logger.warn "You have been rate limited. Retrying in #{seconds_left} seconds..." if @logger

            seconds_left.times do |i|
              sleep 1
              time_left = seconds_left - i
              @logger.warn "#{time_left}..." if time_left > 0 && time_left % 5 == 0 && @logger
            end

            @logger.warn "" if @logger

            @app.call(original_env)
          else
            response
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
zendesk_api-3.1.1 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-3.1.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-3.0.5 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-3.0.4 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-3.0.2 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-3.0.1 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-3.0.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-3.0.0.rc1 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-2.0.1 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-2.0.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.38.0.rc2 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.38.0.rc1 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.37.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.36.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.35.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.34.0 lib/zendesk_api/middleware/request/retry.rb
discourse_zendesk_api-1.0.1 lib/zendesk_api/middleware/request/retry.rb
discourse_zendesk_api-1.0.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.33.0 lib/zendesk_api/middleware/request/retry.rb