Accessing mailbox for actor initialised through pool in Celluloid

Viewed 67

I have 2 actors in my app. Service and PushSocket. I am using mailbox for communication between two actors Service and PushSocket. It works well when I just create single instance on PushSocket and add message to it's mailbox.

**File: service.rb**

Celluloid::ZMQ.init

class Service
  include Celluloid::ZMQ
  attr_accessor :pushsocket

  def initialize
    initialize_pushsock_actor
    send_messages
  end

  def initialize_pushsock_actor
    @pushsocket = PushSocket.new
  end

  def send_messages
    10.times do
      puts 'sending data'
      @pushsocket.mailbox << 'test'
    end
  end
end

**File: push_socket.rb**

Celluloid::ZMQ.init
class PushSocket
  include Celluloid::ZMQ

  def initialize
    async.wait_for_my_messages
  end

  def wait_for_my_messages
    loop do
      message = receive { |msg| msg }
      puts "Got a Message: #{message.inspect}"
    end
  end
end

But when try same with pool it doesn't work as expected. I do not receive any message in push socket.

**File: service.rb**

Celluloid::ZMQ.init
class Service
  include Celluloid::ZMQ
  attr_accessor :pushsocket

  def initialize
    initialize_pushsock_actor
    send_messages
  end

  def initialize_pushsock_actor
    @pushsocket = PushSocket.pool(size: 10)
  end

  def send_messages
    10.times do
      puts 'sending data'
      @pushsocket.mailbox << 'test'
    end
  end
end


**File: push_socket.rb**

Celluloid::ZMQ.init
class PushSocket
  include Celluloid::ZMQ

  def initialize
    async.wait_for_my_messages
  end

  def wait_for_my_messages
    loop do
      message = receive { |msg| msg }
      puts "Got a Message: #{message.inspect}"
    end
  end
end

To get this working, I am using instance method of push socket which gives proper results. Not sure whats problem when I try to use mailbox with pool size defined.

1 Answers
Related