From 5451f0c20d2382ac126e923179c096357d20cf0a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 19 Jul 2010 10:09:58 +0000 Subject: event_machine: split out uncommonly used modules Some applications may not use Response*Pipe and TryDefer at all, so there's no reason to pollute the runtime with extra nodes to mark during GC. --- lib/rainbows/event_machine/try_defer.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/rainbows/event_machine/try_defer.rb (limited to 'lib/rainbows/event_machine/try_defer.rb') diff --git a/lib/rainbows/event_machine/try_defer.rb b/lib/rainbows/event_machine/try_defer.rb new file mode 100644 index 0000000..97e384d --- /dev/null +++ b/lib/rainbows/event_machine/try_defer.rb @@ -0,0 +1,27 @@ +# -*- encoding: binary -*- +# :enddoc: + +# Middleware that will run the app dispatch in a separate thread. +# This middleware is automatically loaded by Rainbows! when using +# EventMachine and if the app responds to the +deferred?+ method. +class Rainbows::EventMachine::TryDefer < Struct.new(:app) + # shortcuts + ASYNC_CALLBACK = Rainbows::EvCore::ASYNC_CALLBACK + + def initialize(app) + # the entire app becomes multithreaded, even the root (non-deferred) + # thread since any thread can share processes with others + Rainbows::Const::RACK_DEFAULTS['rack.multithread'] = true + super + end + + def call(env) + if app.deferred?(env) + EM.defer(proc { catch(:async) { app.call(env) } }, env[ASYNC_CALLBACK]) + # all of the async/deferred stuff breaks Rack::Lint :< + nil + else + app.call(env) + end + end +end -- cgit v1.2.3-24-ge0c7