开发者

http.get randomly says "getaddrinfo: Name or service not known"

To generate offline reports, my Rails app needs to download charts from the Google Charts API.

PROBLEM: It works fine most of the time, but sometimes (randomly) it fails and says getaddrinfo: Name or service not known

When I get the error, I just relaunch the generation and usually it is succ开发者_StackOverflow社区essful.

Is it usual? Is there a best practice to protect against this?

Maybe a re-entrant algorithm, or a more high-level method?

Current code:

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


... it seems, you are overloading your DNS mechanism. Try this:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜