【问题标题】:Typhoeus does not close https connectionsTyphoeus 不会关闭 https 连接
【发布时间】:2012-09-15 14:10:40
【问题描述】:

我正在使用 Typhoeus 处理我网站上对外部 API 的所有 HTTP 调用,直到最近它一直运行良好。一段时间后,我的 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 以在连接完成后强制关闭连接?

【问题讨论】:

    标签: ruby-on-rails http https connection-pooling typhoeus


    【解决方案1】:

    您使用的是哪个操作系统和 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

    您的代码将类似于 Typhoeus 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
    

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      相关资源
      最近更新 更多