Class: DCell::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/dcell/router.rb

Overview

Route incoming messages to their recipient actors

Class Method Summary collapse

Class Method Details

.find(mailbox_address) ⇒ Object

Find a mailbox by its address



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dcell/router.rb', line 22

def find(mailbox_address)
  @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

.gcObject

Prune all entries that point to dead objects



37
38
39
40
41
42
43
# File 'lib/dcell/router.rb', line 37

def gc
  @mutex.synchronize do
    @mailboxes.each do |id, ref|
      @mailboxes.delete id unless ref.weakref_alive?
    end
  end
end

.register(mailbox) ⇒ Object

Enter a mailbox into the registry



11
12
13
14
15
16
17
18
19
# File 'lib/dcell/router.rb', line 11

def register(mailbox)
  @mutex.synchronize do
    address = mailbox.address
    ref = @mailboxes[address]
    @mailboxes[address] = WeakRef.new(mailbox) unless ref && ref.weakref_alive?

    address
  end
end