Sha256: 08aac2376e52a766a45203be71adbf8e1c8d3096bc0b7c396dd4613ee9642b38

Contents?: true

Size: 1.14 KB

Versions: 23

Compression:

Stored size: 1.14 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
        ERROR_CODES = [429, 503]

        def initialize(app, options = {})
          super(app)
          @logger = options[:logger]
        end

        def call(env)
          original_env = env.dup
          response = @app.call(env)

          if ERROR_CODES.include?(response.env[:status])
            seconds_left = (response.env[:response_headers][:retry_after] || DEFAULT_RETRY_AFTER).to_i
            @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

23 entries across 23 versions & 1 rubygems

Version Path
zendesk_api-1.32.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.31.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.30.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.29.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.28.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.27.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.26.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.25.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.24.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.23.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.22.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.21.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.20.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.19.1 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.19.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.18.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.17.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.16.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.15.0 lib/zendesk_api/middleware/request/retry.rb
zendesk_api-1.14.4 lib/zendesk_api/middleware/request/retry.rb