http.get随机说“getaddrinfo:名称或服务未知”

时间:2011-09-29 06:07:43

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

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

1 个答案:

答案 0 :(得分: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