Class: Concurrent::Actor::Utils::Balancer

Inherits:
RestartingContext show all
Defined in:
lib/concurrent-ruby-edge/concurrent/actor/utils/balancer.rb

Overview

Distributes messages between subscribed actors. Each actor'll get only one message then it's unsubscribed. The actor needs to resubscribe when it's ready to receive next message. It will buffer the messages if there is no worker registered.

See Also:

Instance Method Summary collapse

Constructor Details

#initializeBalancer

Returns a new instance of Balancer.



13
14
15
16
# File 'lib/concurrent-ruby-edge/concurrent/actor/utils/balancer.rb', line 13

def initialize
  @receivers = []
  @buffer    = []
end

Instance Method Details

#distributeundocumented



37
38
39
40
41
# File 'lib/concurrent-ruby-edge/concurrent/actor/utils/balancer.rb', line 37

def distribute
  while !@receivers.empty? && !@buffer.empty?
    redirect @receivers.shift, @buffer.shift
  end
end

#on_message(message) ⇒ undocumented



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/concurrent-ruby-edge/concurrent/actor/utils/balancer.rb', line 18

def on_message(message)
  command, who = message
  case command
  when :subscribe
    @receivers << (who || envelope.sender)
    distribute
    true
  when :unsubscribe
    @receivers.delete(who || envelope.sender)
    true
  when :subscribed?
    @receivers.include?(who || envelope.sender)
  else
    @buffer << envelope
    distribute
    Behaviour::MESSAGE_PROCESSED
  end
end