Slowpoke
Rack::Timeout enhancements for Rails
- custom error pages
- safer service timeouts
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.
Trying It in Development
Temporarily add to config/environments/development.rb
:
config.slowpoke.timeout = 1
config.consider_all_requests_local = false
And add a sleep
call to one of your actions:
sleep(2)
The custom error page should appear.
Configuring It for Production
The default timeout is 15 seconds. You can change this in config/environments/production.rb
with:
config.slowpoke.timeout = 5
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
It’s a good idea to set a statement timeout and a connect timeout.
Upgrading
0.3.0
If you set the timeout with:
Slowpoke.timeout = 5
Remove it and add to config/environments/production.rb
:
config.slowpoke.timeout = 5
If you use migration timeouts, check out this guide for how to configure them directly in config/database.yml
.
0.1.0
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:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features