Module: HTTPX::Plugins::RateLimiter
- Defined in:
- lib/httpx/plugins/rate_limiter.rb
Overview
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
Defined Under Namespace
Modules: InstanceMethods
Constant Summary collapse
- RATE_LIMIT_CODES =
[429, 503].freeze
Class Method Summary collapse
- .load_dependencies(klass) ⇒ Object
-
.retry_after_rate_limit(_, response) ⇒ Object
Servers send the “Retry-After” header field to indicate how long the user agent ought to wait before making a follow-up request.
Class Method Details
.load_dependencies(klass) ⇒ Object
18 19 20 |
# File 'lib/httpx/plugins/rate_limiter.rb', line 18 def load_dependencies(klass) klass.plugin(:retries, retry_after: method(:retry_after_rate_limit)) end |
.retry_after_rate_limit(_, response) ⇒ Object
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.
30 31 32 33 34 35 36 37 38 |
# File 'lib/httpx/plugins/rate_limiter.rb', line 30 def retry_after_rate_limit(_, response) return unless response.is_a?(Response) retry_after = response.headers["retry-after"] return unless retry_after Utils.parse_retry_after(retry_after) end |