Class: Concurrent::Actor::Behaviour::Linking

Inherits:
Abstract
  • Object
show all
Defined in:
lib/concurrent/actor/behaviour/linking.rb

Overview

Links the actor to other actors and sends actor's events to them, like: :terminated, :paused, :resumed, errors, etc. Linked actor needs to handle those messages.

listener = AdHoc.spawn name: :listener do
  lambda do |message|
    case message
    when Reference
      if message.ask!(:linked?)
        message << :unlink
      else
        message << :link
      end
    else
      puts "got event #{message.inspect} from #{envelope.sender}"
    end
  end
end

an_actor = AdHoc.spawn name: :an_actor, supervise: true, behaviour_definition: Behaviour.restarting_behaviour_definition do
  lambda { |message| raise 'failed'}
end

# link the actor
listener.ask(an_actor).wait
an_actor.ask(:fail).wait
# unlink the actor
listener.ask(an_actor).wait
an_actor.ask(:fail).wait
an_actor << :terminate!

produces only two events, other events happened after unlinking

got event #<RuntimeError: failed> from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>
got event :reset from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Linking) initialize(core, subsequent, core_options)

Returns a new instance of Linking



43
44
45
46
47
# File 'lib/concurrent/actor/behaviour/linking.rb', line 43

def initialize(core, subsequent, core_options)
  super core, subsequent, core_options
  @linked = Set.new
  @linked.add Actor.current if core_options[:link] != false
end

Instance Attribute Details

- (undocumented) core (readonly) Originally defined in class Abstract

- (undocumented) subsequent (readonly) Originally defined in class Abstract

Instance Method Details



64
65
66
67
# File 'lib/concurrent/actor/behaviour/linking.rb', line 64

def link(ref)
  @linked.add(ref)
  true
end

- (undocumented) on_envelope(envelope)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/concurrent/actor/behaviour/linking.rb', line 49

def on_envelope(envelope)
  case envelope.message
  when :link
    link envelope.sender
  when :unlink
    unlink envelope.sender
  when :linked?
    @linked.include? envelope.sender
  when :linked
    @linked.to_a
  else
    pass envelope
  end
end

- (undocumented) on_event(public, event)



74
75
76
77
78
79
# File 'lib/concurrent/actor/behaviour/linking.rb', line 74

def on_event(public, event)
  event_name, _ = event
  @linked.each { |a| a << event } if public
  @linked.clear if event_name == :terminated
  super public, event
end


69
70
71
72
# File 'lib/concurrent/actor/behaviour/linking.rb', line 69

def unlink(ref)
  @linked.delete(ref)
  true
end