Skip to content

Commit c147fb0

Browse files
author
Petr Chalupa
committed
Merge pull request #156 from ruby-concurrency/actress
Doc updates
2 parents 2f9278a + ff2a170 commit c147fb0

File tree

10 files changed

+54
-31
lines changed

10 files changed

+54
-31
lines changed

doc/actor/quick.in.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,37 @@ def on_message(message)
2929
counter.ask(0).value
3030

3131
# Terminate the actor.
32-
counter.tell(:terminate!)
32+
counter.tell :terminate!
3333
# Not terminated yet, it takes a while until the message is processed.
34-
counter.terminated?
34+
counter.ask! :terminated?
3535
# Waiting for the termination.
36-
counter.terminated.class
37-
counter.terminated.wait
38-
counter.terminated?
36+
event = counter.ask!(:terminated_event)
37+
event.class
38+
event.wait
39+
counter.ask! :terminated?
3940
# Any subsequent messages are rejected.
4041
counter.ask(5).wait.rejected?
4142

4243
# Failure on message processing terminates the actor.
4344
counter = Counter.spawn(:first, 0)
4445
counter.ask('boom').wait.rejected?
45-
counter.terminated?
46+
counter.ask! :terminated?
4647

4748

4849
# Lets define an actor creating children actors.
4950
class Node < Concurrent::Actor::Context
51+
def initialize
52+
@last_child_id = 0
53+
end
54+
5055
def on_message(message)
5156
case message
5257
when :new_child
53-
Node.spawn :child
58+
Node.spawn "child-#{@last_child_id += 1}"
5459
when :how_many_children
5560
children.size
5661
else
57-
raise 'unknown'
62+
pass
5863
end
5964
end
6065
end #
@@ -70,5 +75,5 @@ def on_message(message)
7075

7176
# Termination of an parent will also terminate all children.
7277
parent.ask('boom').wait #
73-
parent.terminated?
74-
child.terminated?
78+
counter.ask! :terminated?
79+
counter.ask! :terminated?

doc/actor/quick.out.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,46 @@ def on_message(message)
2929
counter.ask(0).value # => 7
3030

3131
# Terminate the actor.
32-
counter.tell(:terminate!) # => #<Concurrent::Actor::Reference /first (Counter)>
32+
counter.tell :terminate! # => #<Concurrent::Actor::Reference /first (Counter)>
3333
# Not terminated yet, it takes a while until the message is processed.
34-
counter.terminated? # => false
34+
counter.ask! :terminated? # => true
3535
# Waiting for the termination.
36-
counter.terminated.class # => Concurrent::Event
37-
counter.terminated.wait # => true
38-
counter.terminated? # => true
36+
event = counter.ask!(:terminated_event)
37+
# => #<Concurrent::Event:0x007f6208da3d30 @set=true, @mutex=#<Mutex:0x007f6208da3ce0>, @condition=#<Concurrent::Condition:0x007f6208da3cb8 @condition=#<Thread::ConditionVariable:0x007f6208da3c90>>>
38+
event.class # => Concurrent::Event
39+
event.wait # => true
40+
counter.ask! :terminated? # => true
3941
# Any subsequent messages are rejected.
4042
counter.ask(5).wait.rejected? # => true
4143

4244
# Failure on message processing terminates the actor.
4345
counter = Counter.spawn(:first, 0) # => #<Concurrent::Actor::Reference /first (Counter)>
4446
counter.ask('boom').wait.rejected? # => false
45-
counter.terminated? # => false
47+
counter.ask! :terminated? # => false
4648

4749

4850
# Lets define an actor creating children actors.
4951
class Node < Concurrent::Actor::Context
52+
def initialize
53+
@last_child_id = 0
54+
end
55+
5056
def on_message(message)
5157
case message
5258
when :new_child
53-
Node.spawn :child
59+
Node.spawn "child-#{@last_child_id += 1}"
5460
when :how_many_children
5561
children.size
5662
else
57-
raise 'unknown'
63+
pass
5864
end
5965
end
6066
end
6167

6268
# Actors are tracking parent-child relationships
6369
parent = Node.spawn :parent # => #<Concurrent::Actor::Reference /parent (Node)>
6470
child = parent.tell(:new_child).ask!(:new_child)
65-
# => #<Concurrent::Actor::Reference /parent/child (Node)>
71+
# => #<Concurrent::Actor::Reference /parent/child-2 (Node)>
6672
child.parent # => #<Concurrent::Actor::Reference /parent (Node)>
6773
parent.ask!(:how_many_children) # => 2
6874

@@ -72,5 +78,5 @@ def on_message(message)
7278

7379
# Termination of an parent will also terminate all children.
7480
parent.ask('boom').wait
75-
parent.terminated? # => true
76-
child.terminated? # => true
81+
counter.ask! :terminated? # => false
82+
counter.ask! :terminated? # => false

lib/concurrent/actor.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
require 'concurrent/atomic/synchronization'
66

77
module Concurrent
8-
# TODO https://github.com/celluloid/celluloid/wiki/Supervision-Groups
8+
# TODO https://github.com/celluloid/celluloid/wiki/Supervision-Groups ?
9+
# TODO Remote actors using DRb
10+
# TODO IO interoperation
11+
# TODO un/become
912

1013
# TODO doc
1114
# - what happens if I try to supervise using a normal Context?
12-
15+
# - how to change behaviours
16+
# - how to implement custom restarting?
17+
# - pool for io operations using different executor
18+
# - document guaranteed ordering
1319

1420
# {include:file:doc/actor/main.md}
1521
module Actor
@@ -77,7 +83,7 @@ def self.spawn!(*args, &block)
7783
end
7884

7985
# @overload spawn_optionify(context_class, name, *args)
80-
# @param [Context] context_class to be spawned
86+
# @param [AbstractContext] context_class to be spawned
8187
# @param [String, Symbol] name of the instance, it's used to generate the {Core#path} of the actor
8288
# @param args for context_class instantiation
8389
# @overload spawn_optionify(opts)

lib/concurrent/actor/behaviour/errors_on_unknown_message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Concurrent
22
module Actor
33
module Behaviour
4-
# Simply fails when message arrives here.
4+
# Simply fails when message arrives here. It's usually the last behaviour.
55
class ErrorsOnUnknownMessage < Abstract
66
def on_envelope(envelope)
77
raise UnknownMessage, envelope

lib/concurrent/actor/behaviour/linking.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ module Concurrent
22
module Actor
33
module Behaviour
44

5-
# Links the actor to other actors and sends events to them,
5+
# Links the actor to other actors and sends actor's events to them,
66
# like: `:terminated`, `:paused`, errors, etc
7+
# TODO example
78
class Linking < Abstract
89
def initialize(core, subsequent)
910
super core, subsequent
@@ -16,6 +17,8 @@ def on_envelope(envelope)
1617
link envelope.sender
1718
when :unlink
1819
unlink envelope.sender
20+
when :linked?
21+
@linked.include? envelope.sender
1922
else
2023
pass envelope
2124
end

lib/concurrent/actor/behaviour/pausing.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Behaviour
55
# Allows to pause actors on errors.
66
# When paused all arriving messages are collected and processed after the actor
77
# is resumed or reset. Resume will simply continue with next message.
8-
# Reset also reinitialized context. `:reset!` and `:resume!` messages are only accepted
9-
# form supervisor, see Supervised behaviour.
8+
# Reset also reinitialized context.
9+
# TODO example
1010
class Pausing < Abstract
1111
def initialize(core, subsequent)
1212
super core, subsequent

lib/concurrent/actor/behaviour/supervised.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module Actor
33
module Behaviour
44

55
# Sets and holds the supervisor of the actor if any. There is at most one supervisor
6-
# for each actor. Each supervisor is automatically linked.
6+
# for each actor. Each supervisor is automatically linked. Messages:
7+
# `:pause!, :resume!, :reset!, :restart!` are accepted only from supervisor.
78
class Supervised < Abstract
89
attr_reader :supervisor
910

lib/concurrent/actor/behaviour/termination.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def on_envelope(envelope)
4444
def terminate!
4545
return true if terminated?
4646
terminated.set
47-
broadcast(:terminated)
47+
broadcast(:terminated) # TODO do not end up in Dead Letter Router
4848
parent << :remove_child if parent
4949
true
5050
end

lib/concurrent/actor/context.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def self.spawn_optionify(name_or_opts, *args)
115115
undef_method :spawn
116116
end
117117

118-
# Basic Context of an Actor.
118+
# Basic Context of an Actor. It does not support supervision and pausing.
119+
# It simply terminates on error.
119120
#
120121
# - linking
121122
# - terminates on error

lib/concurrent/actor/utils/balancer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Utils
44

55
# Distributes messages between subscribed actors. Each actor'll get only one message then
66
# it's unsubscribed. The actor needs to resubscribe when it's ready to receive next message.
7+
# It will buffer the messages if there is no worker registered.
78
# @see Pool
89
class Balancer < RestartingContext
910

0 commit comments

Comments
 (0)