Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout for a particular URL in rails

I use rack-timeout and it works fine. But I couldn't figure out how to set time for a particular URL.

Even if I do like:

map '/foo/bar' do
  Rack::Timeout.timeout = 10
end

not only /foo/bar action but every action dies after 10 second.

Is it possible to set timeout for a particular URL? Or should I use another solution other than rack-timeout?

like image 523
chikaram Avatar asked Feb 16 '23 15:02

chikaram


1 Answers

If you're worried about particular actions running too long, I would wrap the code of concern in a Timeout block instead of trying to enforce timeouts on a URL level. You could easily wrap up the below into a helper method and use it with a variable timeout throughout your controllers.

require "timeout'"
begin
  status = Timeout::timeout(10) {
  # Potentially long process here...
}
rescue Timeout::Error
  puts 'This is taking way too long.'
end
like image 56
Lawson Kurtz Avatar answered Feb 28 '23 13:02

Lawson Kurtz