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

Inherits:
Abstract
  • Object
show all
Defined in:
lib/concurrent-ruby-edge/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

Attributes inherited from Abstract

#core, #subsequent

Instance Method Summary collapse

Methods inherited from Abstract

#broadcast, #pass, #reject_envelope

Methods included from InternalDelegations

#behaviour, #behaviour!, #children, #context, #dead_letter_routing, #log, #redirect, #terminate!, #terminated?

Methods included from PublicDelegations

#context_class, #executor, #name, #parent, #path, #reference

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?

Constructor Details

#initialize(core, subsequent, core_options) ⇒ Linking

Returns a new instance of Linking.



45
46
47
48
49
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 45

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 Method Details



66
67
68
69
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 66

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

#on_envelope(envelope) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 51

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

#on_event(public, event) ⇒ Object



76
77
78
79
80
81
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 76

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


71
72
73
74
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 71

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