Guest User

Untitled

a guest
May 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. require 'thread'
  2.  
  3. module Rack
  4. class Middleware
  5. # This middleware will kill long running requests
  6. class SoftTimeout
  7. def initialize(app, options = {})
  8. @app = app
  9. @max_time = options[:max_time] || 50
  10. end
  11. def call(env)
  12. status=headers=response = nil
  13.  
  14. current_thread = Thread.current
  15.  
  16. watcher = Thread.new do
  17. sleep(@max_time)
  18. current_thread.raise("ABORT, Too much time spent on request")
  19. end
  20.  
  21. status, headers, response = @app.call(env)
  22. # Is watcher still running ?
  23. watcher.terminate
  24.  
  25. [status, headers, response]
  26. end
  27. end
  28. end
  29. end
Add Comment
Please, Sign In to add comment