Timeouts in Rails/Nginx — Best Practices

2019-07-17 03:43发布

问题:

I'm having working on a Rails application that is supposed to run on an Nginx server. Depending on the input, the application may take very long to process the requests (or hang up in case of bugs), so I want to prevent processes from running forever. Apart from the Nginx configuration, which ensures that the client gets a timeout signal, I think I still may have to make sure that my app stops working on those requests. Where should this be handled? Within my Rails app (using Timeout/Thread from std-lib) or externally (killing processes)?

回答1:

I usually do this at the Rack layer using Rack::Timeout.

# config/initializers/timeout.rb
Rack::Timeout.timeout = 10  # seconds