【问题标题】:Faraday (Ruby) Timeout Errors法拉第 (Ruby) 超时错误
【发布时间】:2012-03-24 22:30:21
【问题描述】:

我正在尝试使用 Faraday 将路由 A(Sinatra 应用程序)中生成的小负载放到路由 B。所以代码基本上是这样的:

post "/routeA" do
  foo.save
  foo_id = foo.id
  conn = Faraday.new(:url => "http://localhost:3001/routeB" ) do |builder|
    builder.request :url_encoded
    builder.response :logger
    builder.adapter :net_http
  end

  resp = conn.put do |req|
    req.url '/routeB'
    req.headers['Content-Type'] = 'application/json'
    req.body = {:id => foo_id }.to_json
    req.options = {
      #:timeout => 5,   # see below, these aren't the problem
      #:open_timeout => 2
    }
  end

  # never gets here b/c Timeout error always thrown
  STDERR.puts resp.body
end

put "/routeB" do
  # for test purposes just log output
  STDERR.puts request.body.read.to_s.inspect
  status 202
  body '{"Ok"}'
end

问题是它总是抛出超时错误(我在没有超时选项的情况下运行,并且使用上面显示的选项 -> 相同的结果)。但是,日志显示请求正在通过:

I, [2012-03-24T16:56:13.241329 #17673]  INFO -- : put http://localhost:3001/routeB
D, [2012-03-24T16:56:13.241427 #17673] DEBUG -- request: Content-Type: "application/json"
#<Faraday::Error::TimeoutError>
DEBUG -     POST (60.7987ms) /routeA - 500 Internal Server Error
"{\"id\":7}"
DEBUG -      PUT (0.0117ms) /routeB - 202 Accepted

不确定如何克服超时错误?任何见解将不胜感激。谢谢。

【问题讨论】:

    标签: ruby faraday


    【解决方案1】:

    问题是应用程序在完成当前请求之前无法响应另一个请求。也就是说,当您在 /routeB 上发出 PUT 请求时,应用程序得到了它,它正在等待当前请求 (/routeA) 完成。但是请求不会完成,因为它正在等待从/routeB 获得响应。我认为这是导致超时错误的原因。

    【讨论】:

      猜你喜欢
      • 2019-11-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多