Class: Concurrent::Actor::Behaviour::Linking
- 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 ||
case
when Reference
if .ask!(:linked?)
<< :unlink
else
<< :link
end
else
puts "got event #{.inspect} from #{envelope.sender}"
end
end
end
an_actor = AdHoc.spawn name: :an_actor, supervise: true, behaviour_definition: Behaviour.restarting_behaviour_definition do
lambda { || 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
Instance Method Summary collapse
-
#initialize(core, subsequent, core_options) ⇒ Linking
constructor
A new instance of Linking.
- #link(ref) ⇒ Object
- #on_envelope(envelope) ⇒ Object
- #on_event(public, event) ⇒ Object
- #unlink(ref) ⇒ Object
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
Instance Method Details
#link(ref) ⇒ Object
64 65 66 67 |
# File 'lib/concurrent/actor/behaviour/linking.rb', line 64 def link(ref) @linked.add(ref) true end |
#on_envelope(envelope) ⇒ Object
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. 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
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 |
#unlink(ref) ⇒ Object
69 70 71 72 |
# File 'lib/concurrent/actor/behaviour/linking.rb', line 69 def unlink(ref) @linked.delete(ref) true end |