1

ルート A (シナトラ アプリ) で生成された小さなペイロードを、ファラデーを使用してルート 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 に答える