Skip to content

Instantly share code, notes, and snippets.

@cheald
Created February 1, 2014 01:07
Show Gist options
  • Save cheald/8746492 to your computer and use it in GitHub Desktop.
Save cheald/8746492 to your computer and use it in GitHub Desktop.
# Receive a message from the Mailbox
def receive(timeout = nil, &block)
puts "[#{Thread.current.object_id}] Receiving on evented mailbox with timeout of #{timeout}"
message = next_message(block)
until message
puts "[#{Thread.current.object_id}] Got nothing from next_message, looping on the reactor..."
if timeout
# TODO: use hitimes/timers instead of Time.now
now = Time.now
wait_until ||= now + timeout
wait_interval = wait_until - now
raise(TimeoutError, "mailbox timeout exceeded", nil) if wait_interval <= 0
else
wait_interval = nil
end
@reactor.run_once(wait_interval)
message = next_message(block)
end
puts "[#{Thread.current.object_id}] Received message on #{self.to_s}!"
message
rescue IOError
puts "[#{Thread.current.object_id}] Mailbox IOError"
raise MailboxShutdown, "mailbox shutdown called during receive"
end
[12984] Start Runloop
[12984] Receiving message on #<Celluloid::IO::Mailbox:0x25fc02ec> (I am #<Celluloid::Actor:0x4423e76f>)
[12984] Receiving on evented mailbox with timeout of
[12984] Got nothing from next_message, looping on the reactor...
[TID 12984] select result:1
[TID 12984] selectedKeys:[sun.nio.ch.SelectionKeyImpl@45ac6076]
[TID 12984] key readyOps:1
[12984] Got nothing from next_message, looping on the reactor...
[12984] Received message on #<Celluloid::IO::Mailbox:0x25fc02ec>!
[12984] Received message from #<Celluloid::IO::Mailbox:0x25fc02ec> (I am #<Celluloid::Actor:0x4423e76f>)
[12984] Handling message (I am #<Celluloid::Actor:0x4423e76f>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment