法拉第(Ruby)超时错误

时间:2012-03-24 22:30:22

标签: ruby 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

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

1 个答案:

答案 0 :(得分:3)

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

相关问题