Class: EventMachine::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lspace/eventmachine.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allocateObject

Overridden allocate which sets up a new LSpace.

Each connection object is run in its own LSpace, which can be configured by implementing the #setup_lspace method.



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lspace/eventmachine.rb', line 99

def self.allocate
  lspace = @lspace || LSpace.current
  allocate_without_lspace.instance_eval do
    extend EventMachine::LSpacePreserver
    # Create a new LSpace per connection so that connections don't
    # effect each other side-ways.
    LSpace.new({}, lspace).enter do
      setup_lspace
      @lspace = LSpace.current
    end
    self
  end
end

.allocate_without_lspaceObject

As EM uses a custom implementation of new, the only sane way to set up the LSpace in advance is to override allocate.



69
# File 'lib/lspace/eventmachine.rb', line 69

alias_method :allocate_without_lspace, :allocate

.in_lspaceObject

Ensure that instances of this connection are run in the current LSpace

This is used by our version of EM.start_server to ensure that every instance of the server boots inside the same LSpace.

We don’t call it on client classes, so they will inherit the active LSpace when the outbound connection is created.

Examples:

module Handler
  def post_init
    puts LSpace[:error_prefix]
  end
end

LSpace.with(:error_prefix => 'handler') do
  EM::start_server 'localhost', 8080, Handler
end


91
92
93
# File 'lib/lspace/eventmachine.rb', line 91

def self.in_lspace
  @lspace = LSpace.current
end

Instance Method Details

#setup_lspaceObject

Override this method to setup the LSpace in a manner which you require.

This method is called before initialize() and before post_init().

Examples:

module EchoServer
  def setup_lspace
    LSpace[:log_prefix] = rand(100000).to_s(16)
    LSpace.around_filter do |&block|
      begin
        block.call
      rescue => e
        self.rescue(e)
      end
    end
  end

  def rescue(e)
    puts "An exception occurred!: #{e}"
  end
end


134
# File 'lib/lspace/eventmachine.rb', line 134

def setup_lspace; end