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
精彩评论