1

我正在尝试使用 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

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

4

1 回答 1

3

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

于 2012-04-25T18:58:30.430 回答