Net/HTTP Timeouts

Tagged with Ruby Rails

Language: Ruby

View as text

def fetch_something_from_some_api(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = timeout

  response = http.start { http.request(Net::HTTP::Get.new("#{uri.path}?#{uri.query}")) }
  response.is_a?(Net::HTTPSuccess) ? do_something_with(response.body) : nil
rescue Timeout::Error
  logger.warn("Timeout accessing #{uri}: #{$!}")
  nil
rescue
  logger.warn("Error accessing #{uri}: #{$!}")
  nil
end
Last updated at 07:50 AM on Aug 08, 2008 by Brian Hogan

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.