module Rainbows::EventMachine

Implements a basic single-threaded event model with EventMachine. It is capable of handling thousands of simultaneous client connections, but with only a single-threaded app dispatch. It is suited for slow clients, and can work with slow applications via asynchronous libraries such as async_sinatra, Cramp, and rack-fiber_pool.

It does not require your Rack application to be thread-safe, reentrancy is only required for the DevFdResponse body generator.

Compatibility: Whatever EventMachine ~> 0.12.10 and Unicorn both support, currently Ruby 1.8/1.9.

This model is compatible with users of “async.callback” in the Rack environment such as async_sinatra.

For a complete asynchronous framework, Cramp is fully supported when using this concurrency model.

This model is fully-compatible with rack-fiber_pool which allows each request to run inside its own Fiber after all request processing is complete.

Merb (and other frameworks/apps) supporting deferred? execution as documented at Rainbows::EventMachine::TryDefer

This model does not implement as streaming “rack.input” which allows the Rack application to process data as it arrives. This means “rack.input” will be fully buffered in memory or to a temporary file before the application is entered.

RubyGem Requirements

Public Instance Methods

defers_finished?() click to toggle source
# File lib/rainbows/event_machine.rb, line 67
def defers_finished?
  # EventMachine 1.0.0+ has defers_finished?
  EM.respond_to?(:defers_finished?) ? EM.defers_finished? : true
end
em_client_class() click to toggle source

Cramp (and possibly others) can subclass Rainbows::EventMachine::Client and provide the :em_client_class option. We /don't/ want to load Rainbows::EventMachine::Client in the master process since we need reloadability.

# File lib/rainbows/event_machine.rb, line 56
def em_client_class
  case klass = Rainbows::O[:em_client_class]
  when Proc
    klass.call # e.g.: proc { Cramp::WebSocket::Rainbows }
  when Symbol, String
    eval(klass.to_s) # Object.const_get won't resolve multi-level paths
  else # @use should be either :EventMachine or :NeverBlock
    Rainbows.const_get(@use).const_get(:Client)
  end
end