88

I have a Rails application that I'm running on my server. When I go to a remote desktop and attempt to load the application, the server takes a good 3-4 minutes to respond with a simple HTML page. However, when I load up the page locally on the server, the page shows up in just a second. I tried pinging the server from my remote desktop and the pings are going through successful in a reasonable amount of time.

This all seems to have started after I installed Oracle's basic client and SQLPLUS. Should I suspect Oracle? Has anyone experienced anything similar to this?

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173

12 Answers12

139

Having the same issue here (even a year later). Under linux you have to do the following:

Look for the file /usr/lib/ruby/1.9.1/webrick/config.rb and edit it.

Replace the line

:DoNotReverseLookup => nil,

with

:DoNotReverseLookup => true,

Restart webrick and it'll work like a charm :)

Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
  • 21
    Worked! I had problems with Webrick being slow when serving static content from another computer in our local network. This solved it. The only difference was that config.rb was in: ~/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/config.rb - because we are using RVM. – Slobodan Kovacevic Jun 30 '11 at 10:39
  • btw, I didn't have that key, so I just added it and it worked – ecoologic Jul 14 '11 at 14:06
  • where did you add it? what section? – Abe Petrillo Aug 03 '11 at 08:37
  • It is supposed to be in the General section according to http://www.ruby-doc.org/stdlib/libdoc/webrick/rdoc/classes/WEBrick/Config.html. – David Grayson Aug 09 '11 at 19:06
  • 10
    The version of ruby I have is ruby-1.8.7-p330 and it doesn't seem to have the DoNotReverseLookup option. The string "DoNotReverseLookup" does not appear in webrick's config.rb or anywhere in ~/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8. Is there any nice way to fix this problem in ruby-1.8.7-p330? – David Grayson Aug 09 '11 at 19:08
  • This is fantastic. I've been dealing with this on VirtualBox for months, now. Thanks! – techpeace Sep 26 '11 at 08:42
  • Another thanks! This should be in the Rails Guides as a "TIP" ! – Zabba Oct 28 '11 at 20:22
  • It worked. I found the Webrick config file at /usr/local/lib/ruby/1.9.1/webrick/config.rb I was having a very slow connection to the rails server that is hosted in a VirtualBox ubuntu guest. I am accessing the web app from the host OS using port forwarding. – Leonel Machava Nov 22 '12 at 10:37
  • Just in case you won't scroll the few lines below this answer, here you go for RVM: ~/.rvm/rubies/ruby-/lib/ruby//webrick/config.rb – Kjellski Jan 10 '13 at 20:33
  • w◉◡◉t! ttttttttttyyyyyyyyyy <3 – Wesley Schleumer de Góes Aug 10 '13 at 13:21
  • This fix didn't work for me, it turns out that VirtualBox shared folders via vboxfs are also an issue: http://stackoverflow.com/questions/15295298/rails-very-slow-in-development-using-ubuntu-vvm/18315468#18315468 – fotinakis Sep 30 '13 at 23:23
  • Thanks! On Windows boxes that uses [RubyInstaller](http://rubyinstaller.org/), `config.rb` can be found at `C:\Ruby193\lib\ruby\1.9.1\webrick` – Hari Pachuveetil Feb 18 '14 at 18:26
  • @FloydPink that actually should be `C:\RailsInstaller\Ruby1.9.3\lib\ruby\1.9.1\webrick` on Windows with rails set up using RailsInstaller. – Rápli András Apr 28 '14 at 19:14
  • Did not solve my problem. Prof. Falken's answer below did. – Edi Bice Mar 02 '16 at 20:24
  • @DavidGrayson for 1.8.x ruby, see my answer below using: Socket.do_not_reverse_lookup – ohho Dec 08 '16 at 13:54
36

Had the same problem. For me, this post held the solution. If you are on Ubuntu, stop (or uninstall) the avahi-daemon. service avahi-daemon stop stops the daemon.

Webrick now feels very speedy.

The problem has an old report in Rails Lighthouse, however, Ruby-on-Rails have moved their tickets to github since then; Kind of unfortunate that this old problem persists still.

Be aware though, that if you actually use avahi-daemon for something, like finding printers and scanners on your network, that won’t work anymore.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
23

Just had the same problem. The

...
:DoNotReverseLookup => true,
...

did the trick for me too. Just in case you´re running ruby under the rvm, here is the path to go for:

~/.rvm/rubies/ruby-<version>/lib/ruby/<version>/webrick/config.rb
Kjellski
  • 925
  • 10
  • 24
  • 1
    Thanks! Before I found your answer, I searched .rvm and didn't find webrick, and changed the /usr/lib one and that didn't help. This worked. – B Seven Jun 14 '11 at 18:27
  • @KenBarber Pretty sure it's not a good direction, but in order to have a nice and small development-cycle it's okay to do just this modification to your local RVM installation. And by the way, it's just your users profile, you won't bother anyone else... – Kjellski Jan 10 '13 at 20:32
  • 1
    @Kjellski perhaps you are right, but it doesn't persist through upgrades, nor is it a solution you can easily pass around to your fellow devs. Perhaps a monkey patch on Rails in this case is better *shrug*. Anyway its fixed now: https://github.com/rails/rails/commit/4b21a2fd12488731a19104086d1730d37e0b87bb ... – Ken Barber Jan 11 '13 at 00:04
15

"Thin" is now a great option for running both locally and on Heroku:

On Heroku: https://devcenter.heroku.com/articles/rails3#webserver

Website: http://code.macournoyer.com/thin/

You can use it locally by putting in your Gemfile:

gem "thin"

... and then run bundle and start your server with thin start or rails s.

Update on Heroku

Thin is now considered a bad choice for Heroku. More information here:

https://blog.heroku.com/archives/2013/4/3/routing_and_web_performance_on_heroku_a_faq

Their recommendation:

Switch to a concurrent web backend like Unicorn or Puma on JRuby, which allows the dyno to manage its own request queue and avoid blocking on long requests.

Jamon Holmgren
  • 23,738
  • 6
  • 59
  • 75
  • A perfect solution by not changing codes or anything in system. – Fernando Kosh Jan 09 '14 at 23:25
  • Turns out Sinatra uses thin instead of webrick automatically if it is installed. All I had to do is `gem install thin`. See http://www.sinatrarb.com/intro.html _It is recommended to also run gem install thin, which Sinatra will pick up if available._ EDIT: Drastic performance improvements. From 1.3s to 0.05s. – GuiSim Aug 08 '14 at 03:49
6

I had a vaguely similar problem that manifested itself when accessing a WEBrick server via a VPN. Requests would take a long time, most of it with nothing happening on the wire. Since neither mongrel nor thin gems worked with Ruby1.9 on Windows and there was no way I was getting myself embroiled in compiling stuff from source, I needed to stick with WEBrick.

The fix was to set the config parameter DoNotReverseLookup to true, when creating the WEBrick server:

server = HTTPServer.new {:DoNotReverseLookup => true, ...}
mackenir
  • 10,801
  • 16
  • 68
  • 100
5

You can use Apache or install Thin. In your Gemfile : gem 'thin'

Also you can check the list of web-servers for rails.

Uladz Kha
  • 2,154
  • 4
  • 40
  • 61
2

I experienced 10 seconds delays frequently with Sinatra. This snippet solved it for me.

Add this near the top of your app.rb file

class Rack::Handler::WEBrick
    class << self
        alias_method :run_original, :run
    end
    def self.run(app, options={})
        options[:DoNotReverseLookup] = true
        run_original(app, options)
    end
end

See source

neoneye
  • 50,398
  • 25
  • 166
  • 151
2

Was trying to do this with webrick on 1.8.7 and couldn't find the config to change. However, a cheat you can use is to add to the hosts file of the server which is running webrick the ip address it is trying to reverse lookup..

patrickdavey
  • 1,966
  • 2
  • 18
  • 25
  • Great. This is better then editing some webrick file which needs change after each update. – Petr Jan 04 '12 at 14:04
  • In my case the entry to be added (for the Linux guest running webrick) would be the ip address of the Windows host – prusswan Aug 01 '12 at 19:58
1

This is an old question and answer thread that helped me solve the :DoNotReverseLookup issue on a local development virtual machine and wanted to add additional info. This web page explains the regression error in Ruby core that lead to this issue appearing for some; emphasis is mine; the long an short of all of this is there is a GitHub pull request for a Ruby core fix to this and hopefully it will be approved and merged in a soon-ish release of Ruby:

After a few hours of troubleshooting, it turned out that it was! Apparently, somewhere along the evolution of Ruby's standard lib from 1.8.6 to 2.0.0, WEBrick acquired a new configuration option :DoNotReverseLookup that is set to nil by default. Then, deep in the guts of WEBrick's request processing code, it sets the do_not_reverse_lookup flag on the incoming connection socket instance to the value of config[:DoNotReverseLookup]. Since this value is nil, which is falsy, the effect is the same as setting it to false, overriding the global Socket.do_not_reverse_lookup flag. So, unless you have :DoNotReverseLookup => true in your WEBrick config, reverse DNS lookup will always happen for each new connection, potentially causing serious latency.

Related to this discovery is a GitHub pull request from the author proposing how to repair the issue in the Ruby WEBrick source code: Fix regression bug in WEBrick's :DoNotReverseLookup config option implementation #731

The solution as outlined in the request is to change line 181 in lib/webrick/server.rb from this:

sock.do_not_reverse_lookup = config[:DoNotReverseLookup]

To this:

unless config[:DoNotReverseLookup].nil?

Sharing here if anyone stumbles over this well regarded question/answer thread and are interested in the progress in solving this issue in Ruby core. Hopefully this pull will be merged or the underlying issue be dealt with in some way in the next release of Ruby; maybe 2.1.6?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

In my probably rare situation, it worked after I flushed my iptables, this didn't have any side effects because I didn't have any custom rules (just the default Ubuntu allow all):

sudo iptables -F
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
0

This is a very late answer but I spent a good part of the day debugging this very issue with Rails running on Vagrant. Changing the reverse DNS lookup didn't actually improve request times at all. A combination of two things took my page load from ~20 seconds to ~3 seconds in development mode:

Replace WEBrick with mongrel. I had to use the prerelease version or it wouldn't install:

sudo gem install mongrel --pre

Then add it to my Gemfile for dev:

group :test, :development do
  gem 'mongrel'
end

Started my server like this then:

rails server mongrel -e development

That cut a few seconds off, 5 or 6 seconds, but it was still terribly slow. This was the icing on the cake - add this as well to the Gemfile:

group :development do
  gem 'rails-dev-boost', :git => 'git://github.com/thedarkone/rails-dev-boost.git'
end
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
0

There is no DoNotReverseLookup option in ruby 1.8.x webrick. The solution is to put:

Socket.do_not_reverse_lookup = true

somewhere at the beginning of your script.

Source: WEBrick and Socket.do_not_reverse_lookup: A Tale in Two Acts

ohho
  • 50,879
  • 75
  • 256
  • 383