FaradayMiddleware::Request::Retry
Faraday middleware to manage request retries.
Description
This gem is Faraday middleware that adds retry handling for 429 and 503 errors.
Installation
Add this line to your application's Gemfile:
gem 'faraday_middleware-request-retry'
And then execute:
$ bundle
Or install it yourself as:
$ gem install faraday_middleware-request-retry
Usage
require 'faraday'
require 'faraday_middleware-request-retry'
require 'logger'
conn = Faraday.new(url: "http://example.com") do |builder|
builder.use FaradayMiddleware::Request::Retry, logger: Logger.new(STDOUT), retry_after: 15
builder.adapter Faraday.default_adapter
end
conn.get "/foo" # sends token
Options include:
Option | Values | Optional | Default |
---|---|---|---|
:logger |
Logger object |
optional | |
:retry_after |
Default retry seconds | optional |
10 seconds |
:error_codes |
Array of error codes, e.g. [429, 503, 504]
|
optional | [429, 503] |
External Use
The core retry functionality can be used separately from Faraday when necessary. For example, when requests cannot be replayed, e.g. when Faraday::UploadIO
is used.
res = faraday_client.get '/my_url'
retry_util = FaradayMiddleware::Request::RetryUtil.new
# The following will return `true` for retry, `false` for don't,
# while sleeping when necessary
want_retry = retry_util.retry_status res.status, res.headers['Retry-After']
Change Log
See CHANGELOG.md
Links
Project Repo
Problems, Comments, Suggestions, Contributions?
Any reports of problems, comments or suggestions are most welcome.
Please report these on Github
Credits
This library is originally based on code extracted from the Zendesk Ruby SDK's ZendeskAPI::Middleware::Request::Retry
class, in v1.14.0 (commit 2c2b52e) available under the Apache 2.0 license.
Copyright and license
Copyright © 2015 Zendesk, 2016-2023 John Wang
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.