Sha256: 86bcc5ba25d2604a11bc9560204b2b2fd9930df05d243475b0e25aa1cf8945c7

Contents?: true

Size: 1.11 KB

Versions: 9

Compression:

Stored size: 1.11 KB

Contents

require 'weakref'

module DCell
  # Route incoming messages to their recipient actors
  class Router
    @mutex     = Mutex.new
    @mailboxes = {}

    class << self
      # Enter a mailbox into the registry
      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

      # Find a mailbox by its address
      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

      # Prune all entries that point to dead objects
      def gc
        @mutex.synchronize do
          @mailboxes.each do |id, ref|
            @mailboxes.delete id unless ref.weakref_alive?
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
dcell-0.16.1 lib/dcell/router.rb
stn-dcell-0.16.0 lib/dcell/router.rb
dcell-0.16.0 lib/dcell/router.rb
dcell-0.16.0.pre lib/dcell/router.rb
dcell-0.15.0 lib/dcell/router.rb
dcell-0.15.0.pre lib/dcell/router.rb
dcell-0.14.0 lib/dcell/router.rb
dcell-0.13.0 lib/dcell/router.rb
dcell-0.13.0.pre lib/dcell/router.rb