1

要生成离线报告,我的 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
4

1 回答 1

4

...看来,您正在超载您的 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
于 2011-09-29T08:05:39.083 回答