2

サイトの外部 API へのすべての HTTP 呼び出しを処理するために Typhoeus を使用していますが、最近まで問題なく動作していました。私の Rails サイトは、しばらくすると応答しなくなりました。netstat を実行すると、CLOSE_WAIT 状態の接続が大量にあることに気付きました。それらは次のコードによって生成されます。

  requests = []
  hydra = Typhoeus::Hydra.new
  urls.each_with_index do |url, index|
    request = Typhoeus::Request.new(url, :timeout => 5000)
    request.on_complete do |response|
      begin
        resp = JSON.parse(response.body)

        if resp["error"]
          p "no deal found for #{factual_ids[index]}"
          { :deals => nil }
        else
          { :deals => resp["merchant"]["deals"] }
        end
      rescue Exception => e
        p e.message
        { :deals => nil }
      end
    end

    requests << request
    hydra.queue(request)
  end

  hydra.run

他の HTTP 呼び出しで Typhoeus を使用している方法と異なる唯一の点は、上記の URL がすべて HTTPS の URL であることです。それが重要かどうかはわかりませんが、現時点ではそれしか思い浮かびません。誰もこれを見たことがありますか?完了したら、接続を強制的に閉じるために Typheous に渡すことができるオプションはありますか?

4

1 に答える 1

2

どのオペレーティングシステムとTyphoeusバージョンを使用していますか?ubuntuを持っている人の中には同様の問題に遭遇する人もいるようです。Typhoeus 0.5はまだリリースされていませんが、forbid_reuseオプションをサポートするリリース候補です。

Typhoeus::Request.new(url, :timeout => 5000, :forbid_reuse => true)

これが問題です:https ://github.com/typhoeus/typhoeus/issues/205そしてここにlibcurlのドキュメントがあります:http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTFORBIDREUSE

Typhoeus0.5のコードは次のようになります。

requests = []
Typhoeus.on_complete do |response|
  begin
    resp = JSON.parse(response.body)

    if resp["error"]
      p "no deal found for #{factual_ids[index]}"
      response.options[:response_body] = { :deals => nil }
    else
      response.options[:response_body] = { :deals => resp["merchant"]["deals"] }
    end
  rescue Exception => e
    p e.message
    response.options[:response_body] = { :deals => nil }
  end
end

hydra = Typhoeus::Hydra.new
urls.each_with_index do |url, index|
  request = Typhoeus::Request.new(url, :timeout => 5000)
  requests << request
  hydra.queue(request)
end

hydra.run
于 2012-10-06T20:30:36.563 に答える