6

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..

Milovan Zogovic
  • 1,541
  • 2
  • 16
  • 24
  • We can't help diagnose a code problem without code to see. Net::HTTP doesn't take ten seconds to open connections normally, unless the machine is highly overloaded, or the host you are connecting to is that slow to respond. – the Tin Man Mar 30 '12 at 19:25
  • I've added the sample code. It really doesn't matter what remote URL I request or if the machine is overloaded or not, it always take exactly 10 seconds per request. This is really strange. – Milovan Zogovic Apr 02 '12 at 08:02
  • 2
    Try using ip address instead of name. If the problem is gone, you have issue with dns server. – Ineu Apr 02 '12 at 08:37
  • Thanks @Ineu! That was it. I switched to google public dns and it now works like a charm! – Milovan Zogovic Apr 02 '12 at 13:20

3 Answers3

33

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.

CupraR_On_Rails
  • 2,449
  • 1
  • 19
  • 24
2

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

Milovan Zogovic
  • 1,541
  • 2
  • 16
  • 24
-1

Eventually what solve the problem for me was disabling the Antivirus. I have Norton Security

Natan Rubinstein
  • 665
  • 1
  • 9
  • 27