Skip to content

Commit 465f0ef

Browse files
committed
More documentation
1 parent cd22f67 commit 465f0ef

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

lib/concurrent/actress.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55

66
module Concurrent
77

8-
# @example ping
9-
# class Ping
10-
# include Context
11-
# def on_message(message)
12-
# message
13-
# end
14-
# end
15-
# Ping.spawn(:ping1).ask(:m).value #=> :m
8+
# {include:file:lib/concurrent/actress/doc.md}
169
module Actress
1710

1811
require 'concurrent/actress/type_check'
@@ -47,7 +40,7 @@ def on_message(message)
4740
ROOT = Core.new(parent: nil, name: '/', class: Root).reference
4841

4942
# @param block for actress_class instantiation
50-
# @param args see {#spawn_optionify}
43+
# @param args see {.spawn_optionify}
5144
def self.spawn(*args, &block)
5245
if Actress.current
5346
Core.new(spawn_optionify(*args).merge(parent: Actress.current), &block).reference
@@ -56,7 +49,7 @@ def self.spawn(*args, &block)
5649
end
5750
end
5851

59-
# as {#spawn} but it'll raise when Actor not initialized properly
52+
# as {.spawn} but it'll raise when Actor not initialized properly
6053
def self.spawn!(*args, &block)
6154
spawn(spawn_optionify(*args).merge(initialized: ivar = IVar.new), &block).tap { ivar.no_error! }
6255
end
@@ -66,7 +59,7 @@ def self.spawn!(*args, &block)
6659
# @param [String, Symbol] name of the instance, it's used to generate the path of the actor
6760
# @param args for actress_class instantiation
6861
# @overload spawn_optionify(opts)
69-
# see {Core.new} opts
62+
# see {Core#initialize} opts
7063
def self.spawn_optionify(*args)
7164
if args.size == 1 && args.first.is_a?(Hash)
7265
args.first

lib/concurrent/actress/doc.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Light-weighted implement of Actors. Inspired by Akka and Erlang.
2+
3+
Actors are using a thread-pool by default which makes them very cheap to create and discard.
4+
Thousands of actors can be created allowing to brake the program to small maintainable pieces
5+
without breaking single responsibility principles.
6+
7+
## Quick example
8+
9+
class Counter
10+
include Context
11+
12+
def initialize(initial_value)
13+
@count = initial_value
14+
end
15+
16+
def on_message(message)
17+
case message
18+
when Integer
19+
@count += message
20+
when :terminate
21+
terminate!
22+
else
23+
raise 'unknown'
24+
end
25+
end
26+
end
27+
28+
# create new actor
29+
counter = Counter.spawn(:test_counter, 5) # => a Reference
30+
31+
# send messages
32+
counter.tell(1) # => counter
33+
counter << 1 # => counter
34+
35+
# send messages getting an IVar back for synchronization
36+
counter.ask(0) # => an ivar
37+
counter.ask(0).value # => 7
38+
39+
# terminate the actor
40+
counter.ask(:terminate).wait
41+
counter.terminated? # => true
42+
counter.ask(5).wait.rejected? # => true
43+
44+
# failure on message processing will terminate the actor
45+
counter = Counter.spawn(:test_counter, 0)
46+
counter.ask('boom').wait.rejected? # => true
47+
counter.terminated? # => true
48+
49+
50+
51+
52+
53+

lib/concurrent/actress/reference.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def ask(message, ivar = IVar.new)
3333
message message, ivar
3434
end
3535

36-
# @note can lead to deadlocks
36+
# @note can lead to deadlocks, use only in tests or when you are sure it won't deadlock
3737
# tells message to the actor
3838
# @param [Object] message
3939
# @param [Ivar] ivar to be fulfilled be message's processing result

lib/concurrent/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Concurrent
1111
# A gem-level configuration object.
1212
class Configuration
1313

14+
# a proc defining how to log messages, its interface has to be:
15+
# lambda { |level, progname, message = nil, &block| _ }
1416
attr_accessor :logger
1517

1618
# Create a new configuration object.
@@ -21,6 +23,7 @@ def initialize
2123
@logger = no_logger
2224
end
2325

26+
# if assigned to {#logger}, it will log nothing.
2427
def no_logger
2528
-> (level, progname, message = nil, &block) {}
2629
end

lib/concurrent/logging.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
require 'logger'
22

33
module Concurrent
4+
# Include where logging is needed
45
module Logging
56
include Logger::Severity
67

8+
# Logs through {Configuration#logger}, it can be overridden by setting @logger
9+
# @param [Integer] level one of Logger::Severity constants
10+
# @param [String] progname e.g. a path of an Actor
11+
# @param [String, nil] message when nil block is used to generate the message
12+
# @yields_return [String] a message
713
def log(level, progname, message = nil, &block)
814
(@logger || Concurrent.configuration.logger).call level, progname, message, &block
915
end

0 commit comments

Comments
 (0)