Unclear on how to retry requests
Closed this issue · 2 comments
Basic Info
- Faraday Version: 1.3
- Ruby Version: 2.6.5
Issue description
I am trying to make use of Retry
middleware, but using both documentation and looking online doesn't seem to get it working. I am trying to do a couple of retries if there was a timeout exception or connection failed
Steps to reproduce
This is what I am currently doing:
def connection
@connection ||=
Faraday.new(url: "#{API_HOST}/api/v2", request: { timeout: 3, open_timeout: 1 }) do |f|
f.use Faraday::Request::Retry
f.response :logger
f.request :retry, max: 5, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2, retry_statuses: [429, 408]
end
end
def query
response = connection.get("#{type}/#{id}")
return JSON.parse(response.body, symbolize_names: true) if response.success?
rescue Faraday::TimeoutError, Faraday::ConnectionFailed
end
I've tried without retry_statuses
, by providing exceptions instead, which also didn't work. I've tried with and without f.use Faraday::Request::Retry
as I wasn't sure if that's needed. To see if it's working, I am using webmock and rspec tests:
stub_request(:get, url).to_return(status: 408, body: '').times(4)
expect(query).to eq(nil)
I've also tried using webmock timeout method
stub_request(:get, url).to_timeout.times(4)
I then looked into the logs, to see if more than one request is being made, after first getting a 408:
I, [2021-01-03T13:08:50.611778 #88504] INFO -- request: GET https://www.example.com/api/v2/type/1
I, [2021-01-03T13:08:50.611828 #88504] INFO -- request: User-Agent: "Faraday v1.3.0"
I, [2021-01-03T13:08:50.627447 #88504] INFO -- response: Status 408
I, [2021-01-03T13:08:50.627505 #88504] INFO -- response:
I don't get any more logs, so I presume only a single request gets made and then the test exits. Meaning no retry happens. I am misreading the documentation on how to use this, or specifying configuration incorrect? Or perhaps logs don't show more than a single request?
Hi @doutatsu, the reason you see only one request logged is because the logger
middleware is added BEFORE the retry one. If you swap the two middleware then you might start seeing more logs.
Also, there's no need for the f.use Faraday::Request::Retry
at the beginning, as that is the same as f.request :retry
.
Please give it another try after swapping the middleware and let us know.
One more thing you could try is to provide a retry_block
to the retry middleware, which will be executed before every retry, so you can log additional stuff or put a breakpoint. See (https://lostisland.github.io/faraday/middleware/retry#call-a-block-on-every-retry)