lib/dcell/router.rb in dcell-0.12.0.pre vs lib/dcell/router.rb in dcell-0.13.0.pre

- old
+ new

@@ -2,45 +2,35 @@ module DCell # Route incoming messages to their recipient actors class Router @mutex = Mutex.new - @addresses = {} @mailboxes = {} class << self # Enter a mailbox into the registry def register(mailbox) - @mutex.lock - begin - address = @addresses[mailbox.object_id] - unless address - address = Celluloid.uuid - @addresses[mailbox.object_id] = address - end - + @mutex.synchronize do + address = mailbox.address ref = @mailboxes[address] @mailboxes[address] = WeakRef.new(mailbox) unless ref && ref.weakref_alive? address - ensure - @mutex.unlock rescue nil end end # Find a mailbox by its address def find(mailbox_address) - @mutex.lock - begin - ref = @mailboxes[mailbox_address] - return unless ref - ref.__getobj__ - rescue WeakRef::RefError - # The referenced actor is dead, so prune the registry - @mailboxes.delete mailbox_address - nil - ensure - @mutex.unlock rescue nil + @mutex.synchronize do + begin + ref = @mailboxes[mailbox_address] + return unless ref + ref.__getobj__ + rescue WeakRef::RefError + # The referenced actor is dead, so prune the registry + @mailboxes.delete mailbox_address + nil + end end end # Prune all entries that point to dead objects def gc