サイトの外部 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 に渡すことができるオプションはありますか?