2012-09-24 79 views
1

我使用Typhoeus处理所有对我的网站上的外部API的HTTP调用,直到最近它一直在工作得很好。一段时间后,我的Rails网站开始无响应。我注意到,当我执行netstat时,在CLOSE_WAIT状态下有很多连接,并且它们由以下代码生成。Typhoeus不关闭https连接

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调用使用百头巨怪唯一不同的是,上面的网址却是HTTPS的URL。我不知道这是否有意义,但这是我现在唯一能想到的。有没有人见过这个?是否有一个选项可以传入到Typheous中,一旦完成就强制关闭连接?

回答

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

你的代码是这样,对于百头巨怪0.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 
+0

的感谢!我在Ubuntu上 – Bryan