Skip to content

Instantly share code, notes, and snippets.

@octplane
Created June 9, 2010 13:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save octplane/431451 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment