【问题标题】:Farad​ay Timeout error ReadTimeout when sending user data to remote webserver将用户数据发送到远程 Web 服务器时,法拉第超时错误读取超时
【发布时间】:2014-05-13 05:25:49
【问题描述】:

问题: 当我将数据发布到我们的远程 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'

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

【问题讨论】:

  • 会不会是服务器端的问题?您是否尝试过在 Rails 之外直接向服务器发出请求?
  • 嗨@punkle 好吧,如果这是服务器问题,我认为它会发生更多。在此代码启动的 1000 次中,只有生产中发生法拉第错误的 3 次。所以我认为这不是真正可重现的。

标签: ruby-on-rails ruby web-services faraday


【解决方案1】:

这个超时是 nginx http://nginx.org/en/docs/http/ngx_http_core_module.html#send_timeout 发生的

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

【讨论】:

    猜你喜欢
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多