summary refs log tree commit homepage
path: root/lib/rainbows/event_machine/try_defer.rb
blob: 615adae66df6e1a448c7de7cd9e0df12745971fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- encoding: binary -*-

# 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.
#
# Use EM.threadpool_size in your \Rainbows! config file to control
# the number of threads used by EventMachine.
#
# See http://brainspl.at/articles/2008/04/18/deferred-requests-with-merb-ebb-and-thin
# for more information.
class Rainbows::EventMachine::TryDefer
  # shortcuts
  ASYNC_CALLBACK = Rainbows::EvCore::ASYNC_CALLBACK # :nodoc:

  def initialize(app) # :nodoc:
    # 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
    @app = app
  end

  def call(env) # :nodoc:
    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