1

问题: 当我将数据发布到我们的远程 Web 服务时,我遇到了来自 Faraday 的以下错误。此错误发生在随机用户的随机场合。

错误:

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

更多信息: 我也无法真正重现它,我尝试不将任何内容返回到 rails 但这会产生不同的错误。

当我查看其他 SO 主题时,我没有得到解决这个问题的真正解决方案,有些人建议增加超时时间,但我认为它相当高。

执行请求(执行请求 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

连接:

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

其他版本信息:

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

感谢任何有解决此问题的建议的人。

4

1 回答 1

2

此超时由 nginx http://nginx.org/en/docs/http/ngx_http_core_module.html#send_timeout发生

我们已将超时设置为 120 秒,现在此错误不再发生。

于 2014-06-17T08:01:16.143 回答