Service time is the time taken from when a request first enters rack to when its response is sent back. Rack::Timeout.timeout (or Rack::rvice_timeout) is our principal setting. Require " rack-timeout " use Rack :: Timeout # Call as early as possible so rack-timeout runs before all other middleware. timeout = 5 Sinatra and other Rack apps # insert_before Rack :: Runtime, Rack :: Timeout # customize seconds before timeout # insert middleware wherever you want in the stack Gem " rack-timeout ", require: " rack/timeout/base " # config/initializers/timeout.rb You'll need to do this if you removed Rack::Runtime from the middleware stack, or if you want to determine yourself where in the stack Rack::Timeout gets inserted. To use a custom timeout, create an initializer file: # config/initializers/timeout.rb The middleware is not inserted for the test environment. That'll load rack-timeout and set it up as a Rails middleware using the default timeout of 15s. See the Compatibility section at the end for legacy versions. The following covers currently supported versions of Rails, Rack, Ruby, and Bundler.
#Beyond the rack app trouble code#
App developers should track rack-timeout's data and address recurring instances of particular timeouts, for example by refactoring code so it runs faster or offsetting lengthy work to happen asynchronously.
Rack::Timeout is not a solution to the problem of long-running requests, it's a debug and remediation tool. There's a handful of other settings, read on for details. Most applications will do fine with a setting between 2 and 5 seconds. It's recommended to set the timeout as low as realistically viable for your application. Abort requests that are taking too long an exception is raised.Ī generous timeout of 15s is the default.