Sha256: 9ef13d3c1736d5e49debd620e399f91d07eb264596748611e12d3f1d53b9fc1d

Contents?: true

Size: 1.88 KB

Versions: 7

Compression:

Stored size: 1.88 KB

Contents

module Celluloid
  # An alternative implementation of Celluloid::Mailbox using Reactor
  class EventedMailbox < Celluloid::Mailbox
    attr_reader :reactor

    def initialize(reactor_class)
      super()
      # @condition won't be used in the class.
      @reactor = reactor_class.new
    end

    # Add a message to the Mailbox
    def <<(message)
      @mutex.lock
      begin
        if mailbox_full || @dead
          dead_letter(message)
          return
        end
        if message.is_a?(SystemEvent)
          # SystemEvents are high priority messages so they get added to the
          # head of our message queue instead of the end
          @messages.unshift message
        else
          @messages << message
        end

        current_actor = Thread.current[:celluloid_actor]
        @reactor.wakeup unless current_actor && current_actor.mailbox == self
      rescue IOError
        Logger.crash "reactor crashed", $!
        dead_letter(message)
      ensure
        @mutex.unlock rescue nil
      end
      nil
    end

    # Receive a message from the Mailbox
    def check(timeout = nil, &block)
      # Get a message if it is available and process it immediately if possible:
      if message = next_message(block)
        return message
      end

      # ... otherwise, run the reactor once, either blocking or will return
      # after the given timeout:
      @reactor.run_once(timeout)

      # No message was received:
      return nil
    rescue IOError
      raise MailboxShutdown, "mailbox shutdown called during receive"
    end

    # Obtain the next message from the mailbox that matches the given block
    def next_message(block)
      @mutex.lock
      begin
        super(&block)
      ensure
        @mutex.unlock rescue nil
      end
    end

    # Cleanup any IO objects this Mailbox may be using
    def shutdown
      super do
        @reactor.shutdown
      end
    end
  end
end

Version data entries

7 entries across 5 versions & 4 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/celluloid-0.16.0/lib/celluloid/evented_mailbox.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/celluloid-0.16.0/lib/celluloid/evented_mailbox.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/evented_mailbox.rb
scoot-0.0.4 .bundle/gems/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/evented_mailbox.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/celluloid-0.16.0/lib/celluloid/evented_mailbox.rb
celluloid-0.16.0 lib/celluloid/evented_mailbox.rb
celluloid-0.16.0.pre3 lib/celluloid/evented_mailbox.rb