Skip to content
forked from ankane/slowpoke

Rack::Timeout enhancements for Rails

License

Notifications You must be signed in to change notification settings

bjeanes/slowpoke

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slowpoke

Rack::Timeout enhancements for Rails

Installation

Add this line to your application’s Gemfile:

gem 'slowpoke'

And run:

rails generate slowpoke:install

This creates a public/503.html you can customize.

How to Use

The default timeout is 15 seconds. Change this with:

Slowpoke.timeout = 10

or set:

ENV["REQUEST_TIMEOUT"]

Test by adding a sleep call to one of your actions:

sleep(20)

Subscribe to timeouts with:

ActiveSupport::Notifications.subscribe "timeout.slowpoke" do |name, start, finish, id, payload|
  # report timeout
end

To learn more, see the Rack::Timeout documentation.

Threaded Servers

The only safe way to recover from a request timeout is to spawn a new process. For threaded servers like Puma, this means killing all threads when any one of them times out. This can have a significant impact on performance.

Database Timeouts

For PostgreSQL, set connect and statement timeouts in config/database.yml:

production:
  connect_timeout: 1 # sec
  variables:
    statement_timeout: 250 # ms

To use a different statement timeout for migrations, set:

ENV["MIGRATION_STATEMENT_TIMEOUT"] = 60000 # ms

Test connect timeouts by setting your database host to an unroutable IP.

development:
  host: 10.255.255.1

Test statement timeouts with the pg_sleep function.

SELECT pg_sleep(20);

Upgrading

0.1.0 removes database timeouts, since Rails supports them by default. To restore the previous behavior, use:

production:
  variables:
    statement_timeout: <%= Slowpoke.timeout * 1000 %>

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

About

Rack::Timeout enhancements for Rails

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 75.2%
  • HTML 24.8%