Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby net/http opening connection very slow

Tags:

ruby

net-http

When I'm working with net/http in development it is extremely slow. I've installed net-http-spy gem to get some info about each request and I've found out that "opening connection" part takes the most time (more than 10 seconds). Further more, it doesn't keep connections alive, so it needs to reopen it on every request.

opening connection to maps.google.com...
# ~10 seconds
opened

Is there any way I could somehow improve the performance of net/http library by settings some of its defaults? I don't want a request specific fix, but something that would fix the issue globally. I'm using geokit gem in curent project and I cannot change the way it handles request internally, other than knowing it is using net/http, and it is working great on production (Heroku) but extremely slow in development.

I'm using ruby 1.9.2-p290 on Mac OS X Lion. Ruby is installed and managed with rbenv.


Here is the sample code:

require 'net/http'
require 'uri'

time = Time.now.to_i
uri = URI.parse("http://maps.google.com/maps/geo?q=Chicago&output=xml&oe=utf-8")

Net::HTTP.get(uri)
puts (Time.now.to_i - time)

Net::HTTP.get(uri)
puts (Time.now.to_i - time)

It doesn't really matter what URL I put in there. It always take exactly 10 seconds for each request..

like image 716
Milovan Zogovic Avatar asked Mar 30 '12 08:03

Milovan Zogovic


2 Answers

Old issue, but I recently had the same problem.

It seems to be a problem with certain versions of libc which resolve DNS routes. After spending couple hours to search solution you just need to add require 'resolv-replace' at the beginning of your code.

More information: https://github.com/ruby/ruby/pull/597#issuecomment-40507119

Hope this help somebody in the future.

like image 81
CupraR_On_Rails Avatar answered Oct 14 '22 14:10

CupraR_On_Rails


It was an issue with DNS server. Switched to google public DNS (8.8.8.8, 8.8.4.4). Thanks @Ineu.

like image 28
Milovan Zogovic Avatar answered Oct 14 '22 14:10

Milovan Zogovic