Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'thread'
- module Rack
- class Middleware
- # This middleware will kill long running requests
- class SoftTimeout
- def initialize(app, options = {})
- @app = app
- @max_time = options[:max_time] || 50
- end
- def call(env)
- status=headers=response = nil
- current_thread = Thread.current
- watcher = Thread.new do
- sleep(@max_time)
- current_thread.raise("ABORT, Too much time spent on request")
- end
- status, headers, response = @app.call(env)
- # Is watcher still running ?
- watcher.terminate
- [status, headers, response]
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment