This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Improvements to net-ping
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
I've been refactoring net-ping this weekend, trying to make it stdlib worthy. I've got a working icmp ping class, thanks in part to Jos Backus. I've also been going over the other ping classes, making improvements where I can.
One thing I noticed was that the Ping::HTTP#ping method did not honor the timeout value. For example, this bit of code will always raise a SocketError instead of a TimeoutError:
require 'net/http'
require 'timeout'
require 'uri'
uri = URI.parse("http://www.perldoc.com/index.html")
begin
Timeout.timeout(1){
Net::HTTP.get_response(uri.host, uri.path, 80)
}
rescue Exception => err
puts "ERROR: #{err}"
exit
end
The problem is that Socket.getaddrinfo is blocking by default. So, what's the solution? That came courtesy of RubyPanther on IRC. Just stick "require 'resolv-replace'" at the top of the script and, bang, it works! The 'resolv-replace' library replaces the getaddress methods in some of the various Socket classes with the one from the Resolv class, which is non-blocking.