Mika H.
Mika H.

Reputation: 4339

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?

Upvotes: 5

Views: 2683

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

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

Upvotes: 7

Related Questions