New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when use retry and cache middleware #1238
Comments
Hi @isakovp, could you please share your Faraday connection setup so we can see all the middleware involved and their order in the stack? thanks! |
Faraday.new(options.merge({ url: ENDPOINT_URL,
request: { timeout: 120.second.to_i },
headers: { 'User-Agent' => 'appw.import2.com' } })) do |connection|
connection.request :retry, max: 5, interval: 5.seconds, backoff_factor: 2, exceptions: [
Faraday::Errors::TooManyRequests, # retry api limits
Faraday::Error::ConnectionFailed, Faraday::Error::SSLError, Errno::EPIPE, # connection errors
Errno::ETIMEDOUT, 'Timeout::Error', Faraday::Error::TimeoutError, Faraday::Errors::Timeout, # timeouts
Faraday::Errors::InternalServerError, Faraday::Errors::BadGatewayError, Faraday::Errors::ServiceUnavailable, Faraday::Errors::GatewayTimeout, # server side errors (5xx)
Faraday::Errors::Unauthorized, # retry unauthorized calls -> API token expired
Faraday::Error::ParsingError
]
connection.use Faraday::Response::AdvancedRaiseError
connection.use Faraday::Request::AccessTokenAuthorization, token
connection.request :json
connection.response :json, content_type: 'application/json'
connection.response(:caching, cache) if cache_enabled && cache.present?
connection.response(:logger, Rails.logger, { headers: true, bodies: true }) if http_debug_output?
connection.adapter Faraday.default_adapter
end |
Thanks @isakovp, the first thing I notice is that the connection.response :caching
connection.use Faraday::Response::AdvancedRaiseError
connection.use Faraday::Request::AccessTokenAuthorization
connection.request :json
connection.response :json
connection.request :retry
connection.response :logger
connection.adapter Faraday.default_adapter Could you give this setup a try and let me know if it solves your issue? |
Thanks, @iMacTia it solved my issue, but the code in Caching#finalize_response looks sucks |
Sounds good, @isakovp, does that resolve the issue? |
Basic Info
Issue description
Error when use retry and cache middleware
Steps to reproduce
Job 1 try to do get but failed with 500 error
Job 2 do get and response cached
Job 1 retried and get response from cache, but env[:response_headers] not null, it contains headers from first call and this code ignored
https://github.com/lostisland/faraday_middleware/blob/master/lib/faraday_middleware/response/caching.rb#L115
As temporary fix we use retry_block option for retry middleware
retry_block: Proc.new { |env| env[:response_headers] = nil }
The text was updated successfully, but these errors were encountered: