module HTTPX::Plugins::RateLimiter

  1. lib/httpx/plugins/rate_limiter.rb

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

gitlab.com/os85/httpx/wikis/Rate-Limiter

Constants

RATE_LIMIT_CODES = [429, 503].freeze  

Public Class methods

configure(klass)
[show source]
   # File lib/httpx/plugins/rate_limiter.rb
18 def configure(klass)
19   klass.plugin(:retries,
20                retry_change_requests: true,
21                retry_on: method(:retry_on_rate_limited_response),
22                retry_after: method(:retry_after_rate_limit))
23 end
retry_after_rate_limit(_, response)

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.

[show source]
   # File lib/httpx/plugins/rate_limiter.rb
41 def retry_after_rate_limit(_, response)
42   return unless response.is_a?(Response)
43 
44   retry_after = response.headers["retry-after"]
45 
46   return unless retry_after
47 
48   Utils.parse_retry_after(retry_after)
49 end
retry_on_rate_limited_response(response)
[show source]
   # File lib/httpx/plugins/rate_limiter.rb
25 def retry_on_rate_limited_response(response)
26   return false unless response.is_a?(Response)
27 
28   status = response.status
29 
30   RATE_LIMIT_CODES.include?(status)
31 end