【问题标题】:http.get randomly says "getaddrinfo: Name or service not known"http.get 随机显示“getaddrinfo:名称或服务未知”
【发布时间】:2011-11-27 10:25:24
【问题描述】:

要生成离线报告,我的 Rails 应用需要从 Google Charts API 下载图表。

问题:它大部分时间都可以正常工作,但有时(随机)它会失败并显示getaddrinfo: Name or service not known

当我收到错误时,我只是重新启动生成,通常它会成功。
平常吗?是否有防止这种情况的最佳做法?
可能是重入算法,还是更高级的方法?

当前代码:

require 'net/http'
  charts.each_with_index do |path, index|
    Net::HTTP.start("chart.googleapis.com") do |http|
    resp = http.get(path)
    open("tmp/charts/chart" + index.to_s + ".png" ,"wb") do |file|
        file.write(resp.body)
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails-3 net-http


    【解决方案1】:

    ...看来,您的 DNS 机制超载了。试试这个:

    require 'net/http'
      Net::HTTP.start("chart.googleapis.com") do |http|
        charts.each_with_index do |path, index|
          resp = http.get(path)
          open("tmp/charts/chart" + index.to_s + ".png" ,"wb") do |file|
            file.write(resp.body)
          end
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 2018-09-20
      • 2014-02-18
      • 1970-01-01
      相关资源
      最近更新 更多