ruby net/http opening connection very slow
Asked Answered
C

3

7

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

Charqui answered 30/3, 2012 at 8:48 Comment(4)
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.Syringe
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.Charqui
Try using ip address instead of name. If the problem is gone, you have issue with dns server.Neolamarckism
Thanks @Ineu! That was it. I switched to google public dns and it now works like a charm!Charqui
C
2

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

Charqui answered 14/4, 2012 at 12:15 Comment(0)
H
35

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.

Habitude answered 15/12, 2014 at 13:37 Comment(0)
C
2

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

Charqui answered 14/4, 2012 at 12:15 Comment(0)
L
-1

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

Lowder answered 9/4, 2017 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.