Question

Problem: I'm experiencing the following error from Faraday when I'm posting data to our Remote web service. This error is occurring at random occasions with random users.

Error:

Farad​ay::E​rror:​:Time​outEr​ror: ​Net::​ReadT​imeou​t

More information: I also can't really reproduce it, I tried with not returning anything to rails but this generates a different error.

When I look at other SO topics I don't get a real solution for this problem some suggest upping the timeout time but I think it's pretty high.

Perform Request (performs the request POST/GET):

def perform_request(verb, path, params = nil, body = nil)
  conn = connection.dup
  params ||= {}
  # If params contains a token, it will used the `named` routes and
  # set an authorization header
  if params[:token]
    path = ["named", path].join("/")
    conn.headers["Authorization"] = "Basic #{params.delete(:token)}"
  else
    path = ["anonymous", path].join("/")
  end

  # Remote service expects each body sent to it to be xml-encoded.
  conn.headers["Content-Type"] = "application/xml"
  conn.send(verb, path) do |request|
    log "Path   : #{verb.upcase} #{default_url}#{path}"
    log "Params : #{params.inspect}"
    log "Body   : #{body.inspect[0..1000]}"

    request.params = params if params
    request.body = body if body
    request.options[:timeout] = 120          # open/read timeout in seconds
    # request.options[:open_timeout] = 15      # connection open timeout in seconds
  end
end

The connection:

def connection
  @connection ||=
    begin
      conn = Faraday.new(default_url) do |b|
      b.use FaradayMiddleware::Mashify
      b.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
      b.adapter Faraday.default_adapter
    end
      conn.headers['User-Agent'] = 'be.company.hidden'
      conn
    end
end

Other version information:

gem "faraday", "~> 0.8.1"
gem "faraday_middleware", "~> 0.8.8"
gem 'rails', '3.2.17'

Thanks for anyone who has suggestions on fixing this problem.

Was it helpful?

Solution

This timeout occured by nginx http://nginx.org/en/docs/http/ngx_http_core_module.html#send_timeout

We have set the timeout to 120s and now this error didn't occur again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top