Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rack-timeout gem on Rails

I'm trying to use the rack-timeout gem on Rails. I added the line gem 'rack-timeout' to my Gemfile and ran bundle install. (It shows Using rack-timeout (0.0.4)) Then I submitted a request to my server. It clearly hung for more than 15 seconds without returning anything to me. What am I missing?

like image 343
Mika H. Avatar asked Jun 10 '13 19:06

Mika H.


1 Answers

You need to tell Rails what to do when a Rack::Timeout error is thrown.

  • If you ignore it, execution will stop after 15 seconds (or whatever you configure it to be)
  • If you want to show the user a nice error, you'll need to rescue from that exception (like below).

You could do something like this

class ApplicationController < ActionController::Base

  rescue_from Timeout::Error, with: :handle_timeout

  protected
  def handle_timeout
    render "shared/timeout"
  end
end
like image 104
Jesse Wolgamott Avatar answered Oct 06 '22 22:10

Jesse Wolgamott