Sha256: 530e51757e5cd0d1b4bffc99484c24d1a364f692f17e868cdaa29841bd1235ea

Contents?: true

Size: 1.61 KB

Versions: 39

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Plugins
    #
    # This plugin adds support for retrying requests when the request:
    #
    # * is rate limited;
    # * when the server is unavailable (503);
    # * when a 3xx request comes with a "retry-after" value
    #
    # https://gitlab.com/honeyryderchuck/httpx/wikis/RateLimiter
    #
    module RateLimiter
      class << self
        RATE_LIMIT_CODES = [429, 503].freeze

        def configure(klass)
          klass.plugin(:retries,
                       retry_change_requests: true,
                       retry_on: method(:retry_on_rate_limited_response),
                       retry_after: method(:retry_after_rate_limit))
        end

        def retry_on_rate_limited_response(response)
          status = response.status

          RATE_LIMIT_CODES.include?(status)
        end

        # Servers send the "Retry-After" header field to indicate how long the
        # user agent ought to wait before making a follow-up request.  When
        # sent with a 503 (Service Unavailable) response, Retry-After indicates
        # how long the service is expected to be unavailable to the client.
        # When sent with any 3xx (Redirection) response, Retry-After indicates
        # the minimum time that the user agent is asked to wait before issuing
        # the redirected request.
        #
        def retry_after_rate_limit(_, response)
          retry_after = response.headers["retry-after"]

          return unless retry_after

          Utils.parse_retry_after(retry_after)
        end
      end
    end

    register_plugin :rate_limiter, RateLimiter
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
httpx-0.18.4 lib/httpx/plugins/rate_limiter.rb
httpx-0.18.3 lib/httpx/plugins/rate_limiter.rb
httpx-0.18.2 lib/httpx/plugins/rate_limiter.rb
httpx-0.18.1 lib/httpx/plugins/rate_limiter.rb
httpx-0.18.0 lib/httpx/plugins/rate_limiter.rb
httpx-0.17.0 lib/httpx/plugins/rate_limiter.rb
httpx-0.16.1 lib/httpx/plugins/rate_limiter.rb
httpx-0.16.0 lib/httpx/plugins/rate_limiter.rb
httpx-0.15.4 lib/httpx/plugins/rate_limiter.rb
httpx-0.15.3 lib/httpx/plugins/rate_limiter.rb
httpx-0.15.2 lib/httpx/plugins/rate_limiter.rb
httpx-0.15.1 lib/httpx/plugins/rate_limiter.rb
httpx-0.15.0 lib/httpx/plugins/rate_limiter.rb
httpx-0.14.5 lib/httpx/plugins/rate_limiter.rb
httpx-0.14.4 lib/httpx/plugins/rate_limiter.rb
httpx-0.14.3 lib/httpx/plugins/rate_limiter.rb
httpx-0.14.2 lib/httpx/plugins/rate_limiter.rb
httpx-0.14.1 lib/httpx/plugins/rate_limiter.rb
httpx-0.14.0 lib/httpx/plugins/rate_limiter.rb