Skip to content

Instantly share code, notes, and snippets.

@msroot
Forked from bmarini/net-http-timeouts.rb
Created July 25, 2014 02:28
Show Gist options
  • Select an option

  • Save msroot/93769afe55be1c26b402 to your computer and use it in GitHub Desktop.

Select an option

Save msroot/93769afe55be1c26b402 to your computer and use it in GitHub Desktop.
require "net/http"
http = Net::HTTP.new("example.com")
http.open_timeout = 2
http.read_timeout = 3 # Must be greater than open_timeout
begin
http.start
begin
http.request_get("/whatever?") do |res|
res.read_body
end
rescue Timeout::Error
puts "Timeout due to reading"
end
rescue Timeout::Error
puts "Timeout due to connecting"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment