Skip to content

Instantly share code, notes, and snippets.

@elvuel
Forked from rkh/gist:418380
Created March 6, 2012 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elvuel/1986273 to your computer and use it in GitHub Desktop.
Save elvuel/1986273 to your computer and use it in GitHub Desktop.
class SomeMiddleware
def initialize(app) @app = app end
def call(env)
background(env) { ... }
@app.call env
end
def background(env, &block)
if env['rack.run_once'] then yield
elsif defined? EventMachine then EventMachine.next_tick(&block)
elsif env['rack.multithread'] or not supports_fork? then Thread.new(&block)
else fork(&block)
end
end
def supports_fork?
return @@supports_fork unless @@supports_fork.nil?
@@supports_fork = begin
fork { }
true
rescue NotImplementedError
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment