celluloid 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +37 -482
- data/lib/celluloid.rb +45 -18
- data/lib/celluloid/actor.rb +64 -22
- data/lib/celluloid/actor_pool.rb +1 -1
- data/lib/celluloid/actor_proxy.rb +1 -1
- data/lib/celluloid/application.rb +1 -1
- data/lib/celluloid/calls.rb +2 -2
- data/lib/celluloid/fiber.rb +2 -31
- data/lib/celluloid/fsm.rb +141 -0
- data/lib/celluloid/future.rb +10 -13
- data/lib/celluloid/logger.rb +8 -3
- data/lib/celluloid/mailbox.rb +16 -4
- data/lib/celluloid/receivers.rb +49 -19
- data/lib/celluloid/registry.rb +2 -2
- data/lib/celluloid/signals.rb +12 -10
- data/lib/celluloid/supervisor.rb +7 -4
- data/lib/celluloid/task.rb +53 -0
- data/lib/celluloid/tcp_server.rb +2 -1
- data/lib/celluloid/timers.rb +109 -0
- data/lib/celluloid/version.rb +1 -1
- data/spec/support/actor_examples.rb +453 -0
- data/spec/support/mailbox_examples.rb +52 -0
- metadata +11 -11
- data/lib/celluloid/io.rb +0 -24
- data/lib/celluloid/io/mailbox.rb +0 -65
- data/lib/celluloid/io/reactor.rb +0 -63
- data/lib/celluloid/io/waker.rb +0 -43
data/README.md
CHANGED
@@ -10,7 +10,21 @@ Celluloid provides a simple and natural way to build fault-tolerant concurrent
|
|
10
10
|
programs in Ruby. With Celluloid, you can build systems out of concurrent
|
11
11
|
objects just as easily as you build sequential programs out of regular objects.
|
12
12
|
Recommended for any developer, including novices, Celluloid should help ease
|
13
|
-
your worries about building multithreaded Ruby programs
|
13
|
+
your worries about building multithreaded Ruby programs:
|
14
|
+
|
15
|
+
* __Look ma, no mutexes:__ Celluloid automatically synchronizes access to instance
|
16
|
+
variables by using a special proxy object system and messaging model.
|
17
|
+
* __[Futures](https://github.com/tarcieri/celluloid/wiki/futures):__
|
18
|
+
Ever wanted to call a method "in the background" and retrieve the
|
19
|
+
value it returns later? Celluloid futures allow you to do that. When you
|
20
|
+
ask for a method's return value it's returned if it's immediately available
|
21
|
+
or blocks if the method is still running.
|
22
|
+
* __[Supervisors](https://github.com/tarcieri/celluloid/wiki/supervisors):__
|
23
|
+
Celluloid can monitor your concurrent objects and
|
24
|
+
automatically restart them when they crash. You can also link concurrent
|
25
|
+
objects together into groups that will crash and restart as a group,
|
26
|
+
ensuring that after a crash all interdependent objects are in a clean and
|
27
|
+
consistent state.
|
14
28
|
|
15
29
|
Under the hood, Celluloid wraps regular objects in threads that talk to each
|
16
30
|
other using messages. These concurrent objects are called "actors". When a
|
@@ -25,24 +39,34 @@ In addition to that, Celluloid also gives you the ability to call methods
|
|
25
39
|
_asynchronously_, so the receiver to do things in the background for you
|
26
40
|
without the caller having to sit around waiting for the result.
|
27
41
|
|
42
|
+
You can also build distributed systems with Celluloid using its
|
43
|
+
[sister project DCell](https://github.com/tarcieri/dcell).
|
44
|
+
|
45
|
+
[Please see the Celluloid Wiki](https://github.com/tarcieri/celluloid/wiki)
|
46
|
+
for more detailed documentation and usage notes.
|
47
|
+
|
28
48
|
Like Celluloid? [Join the Google Group](http://groups.google.com/group/celluloid-ruby)
|
29
49
|
|
30
50
|
Supported Platforms
|
31
51
|
-------------------
|
32
52
|
|
33
|
-
Celluloid works on Ruby 1.9.2
|
34
|
-
or Rubinius are the preferred platforms as they support true
|
53
|
+
Celluloid works on Ruby 1.9.2+, JRuby 1.6 (in 1.9 mode), and Rubinius 2.0. JRuby
|
54
|
+
or Rubinius are the preferred platforms as they support true hardware-level
|
55
|
+
parallelism when running Ruby code, whereas MRI/YARV is constrained by a global
|
56
|
+
interpreter lock (GIL).
|
35
57
|
|
36
58
|
To use JRuby in 1.9 mode, you'll need to pass the "--1.9" command line option
|
37
59
|
to the JRuby executable, or set the "JRUBY_OPTS=--1.9" environment variable.
|
38
60
|
|
39
61
|
Celluloid works on Rubinius in either 1.8 or 1.9 mode.
|
40
62
|
|
41
|
-
|
42
|
-
|
63
|
+
Usage
|
64
|
+
-----
|
43
65
|
|
44
66
|
To use Celluloid, define a normal Ruby class that includes Celluloid:
|
45
67
|
|
68
|
+
require 'celluloid'
|
69
|
+
|
46
70
|
class Sheen
|
47
71
|
include Celluloid
|
48
72
|
|
@@ -100,481 +124,11 @@ exceptions will crash the receiver, and making an asynchronous call to a
|
|
100
124
|
crashed object will not raise an error.
|
101
125
|
|
102
126
|
However, you can still handle errors created by asynchronous calls using
|
103
|
-
two features of Celluloid called
|
104
|
-
|
105
|
-
|
106
|
-
Futures
|
107
|
-
-------
|
108
|
-
|
109
|
-
Futures allow you to request a computation and get the result later. There are
|
110
|
-
two types of futures supported by Celluloid: method futures and block futures.
|
111
|
-
Method futures work by invoking the _future_ method on an actor. This method
|
112
|
-
is analogous to the typical _send_ method in that it takes a method name,
|
113
|
-
followed by an arbitrary number of arguments, and a block. Let's invoke the
|
114
|
-
report method from the charlie object used in the above example using a future:
|
115
|
-
|
116
|
-
>> future = charlie.future :report
|
117
|
-
=> #<Celluloid::Future:0x000001009759b8>
|
118
|
-
>> future.value
|
119
|
-
=> "Charlie Sheen is winning!"
|
120
|
-
|
121
|
-
The call to charlie.future immediately returns a Celluloid::Future object,
|
122
|
-
regardless of how long it takes to execute the "report" method. To obtain
|
123
|
-
the result of the call to "report", we call the _value_ method of the
|
124
|
-
future object. This call will block until the value returned from the method
|
125
|
-
call is available (i.e. the method has finished executing). If an exception
|
126
|
-
occured during the method call, the call to future.value will reraise the
|
127
|
-
same exception.
|
128
|
-
|
129
|
-
Futures also allow you to background the computation of any block:
|
130
|
-
|
131
|
-
>> future = Celluloid::Future.new { 2 + 2 }
|
132
|
-
=> #<Celluloid::Future:0x000001008425f0>
|
133
|
-
>> future.value
|
134
|
-
=> 4
|
135
|
-
|
136
|
-
One thing to be aware of when using futures: always make sure to obtain the
|
137
|
-
value of any future you make. Futures create a thread in the background which
|
138
|
-
will continue to run until the future's value is obtained. Failing to obtain
|
139
|
-
the value of futures you create will leak threads.
|
140
|
-
|
141
|
-
Supervisors
|
142
|
-
-----------
|
143
|
-
|
144
|
-
You may be familiar with tools like Monit or God which keep an eye on your
|
145
|
-
applications and restart them when they crash. Celluloid supervisors work in
|
146
|
-
a similar fashion, except instead of monitoring applications, they monitor
|
147
|
-
individual actors and restart them when they crash. Crashes occur whenever
|
148
|
-
an unhandled exception is raised anywhere within an actor.
|
149
|
-
|
150
|
-
To supervise an actor, start it with the _supervise_ method. Using the Sheen
|
151
|
-
class from the example above:
|
152
|
-
|
153
|
-
>> supervisor = Sheen.supervise "Charlie Sheen"
|
154
|
-
=> #<Celluloid::Supervisor(Sheen) "Charlie Sheen">
|
155
|
-
|
156
|
-
This created a new Celluloid::Supervisor actor, and also created a new Sheen
|
157
|
-
actor, giving its initialize method the argument "Charlie Sheen". The
|
158
|
-
_supervise_ method has the same method signature as _new_. However, rather
|
159
|
-
than returning the newly created actor, _supervise_ returns the supervisor.
|
160
|
-
To retrieve the actor that the supervisor is currently using, use the
|
161
|
-
Celluloid::Supervisor#actor method:
|
162
|
-
|
163
|
-
>> supervisor = Sheen.supervise "Charlie Sheen"
|
164
|
-
=> #<Celluloid::Supervisor(Sheen) "Charlie Sheen">
|
165
|
-
>> charlie = supervisor.actor
|
166
|
-
=> #<Celluloid::Actor(Sheen:0x00000100a312d0)>
|
167
|
-
|
168
|
-
Supervisors can also automatically put actors into the actor _registry_ using
|
169
|
-
the supervise_as method:
|
170
|
-
|
171
|
-
>> Sheen.supervise_as :charlie, "Charlie Sheen"
|
172
|
-
=> #<Celluloid::Supervisor(Sheen) "Charlie Sheen">
|
173
|
-
>> charlie = Celluloid::Actor[:charlie]
|
174
|
-
=> #<Celluloid::Actor(Sheen:0x00000100a312d0)>
|
175
|
-
|
176
|
-
In this case, the supervisor will ensure that an actor of the Sheen class,
|
177
|
-
created using the given arguments, is aways available by calling
|
178
|
-
Celluloid::Actor[:charlie]. The first argument to supervise_as is the name
|
179
|
-
you'd like the newly created actor to be registered under. The remaining
|
180
|
-
arguments are passed to initialize just like you called _new_.
|
181
|
-
|
182
|
-
See the "Registry" section below for more information on the actor registry
|
183
|
-
|
184
|
-
Linking
|
185
|
-
-------
|
186
|
-
|
187
|
-
Whenever any unhandled exceptions occur in any of the methods of an actor,
|
188
|
-
that actor crashes and dies. Let's start with an example:
|
189
|
-
|
190
|
-
class JamesDean
|
191
|
-
include Celluloid
|
192
|
-
class CarInMyLaneError < StandardError; end
|
193
|
-
|
194
|
-
def drive_little_bastard
|
195
|
-
raise CarInMyLaneError, "that guy's gotta stop. he'll see us"
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
Now, let's have James drive Little Bastard and see what happens:
|
200
|
-
|
201
|
-
>> james = JamesDean.new
|
202
|
-
=> #<Celluloid::Actor(JamesDean:0x1068)>
|
203
|
-
>> james.drive_little_bastard!
|
204
|
-
=> nil
|
205
|
-
>> james
|
206
|
-
=> #<Celluloid::Actor(JamesDean:0x1068) dead>
|
207
|
-
|
208
|
-
When we told james asynchronously to drive Little Bastard, it killed him! If
|
209
|
-
we were Elizabeth Taylor, co-star in James' latest film at the time of his
|
210
|
-
death, we'd certainly want to know when he died. So how can we do that?
|
211
|
-
|
212
|
-
Actors can _link_ to other actors they're interested in and want to receive
|
213
|
-
crash notifications from. In order to receive these events, we need to use the
|
214
|
-
trap_exit method to be notified of them. Let's look at how a hypothetical
|
215
|
-
Elizabeth Taylor object could be notified that James Dean has crashed:
|
216
|
-
|
217
|
-
class ElizabethTaylor
|
218
|
-
include Celluloid
|
219
|
-
trap_exit :actor_died
|
220
|
-
|
221
|
-
def actor_died(actor, reason)
|
222
|
-
puts "Oh no! #{actor.inspect} has died because of a #{reason.class}"
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
We've now used the trap_exit method to configure a callback which is invoked
|
227
|
-
whenever any linked actors crashed. Now we need to link Elizabeth to James so
|
228
|
-
James' crash notifications get sent to her:
|
229
|
-
|
230
|
-
>> james = JamesDean.new
|
231
|
-
=> #<Celluloid::Actor(JamesDean:0x11b8)>
|
232
|
-
>> elizabeth = ElizabethTaylor.new
|
233
|
-
=> #<Celluloid::Actor(ElizabethTaylor:0x11f0)>
|
234
|
-
>> elizabeth.link james
|
235
|
-
=> #<Celluloid::Actor(JamesDean:0x11b8)>
|
236
|
-
>> james.drive_little_bastard!
|
237
|
-
=> nil
|
238
|
-
Oh no! #<Celluloid::Actor(JamesDean:0x11b8) dead> has died because of a JamesDean::CarInMyLaneError
|
239
|
-
|
240
|
-
Elizabeth called the _link_ method to receive crash events from James. Because
|
241
|
-
Elizabeth was linked to James, when James crashed, James' exit message was
|
242
|
-
sent to her. Because Elizabeth was trapping the exit messages she received
|
243
|
-
using the trap_exit method, the callback she specified was invoked, allowing
|
244
|
-
her to take action (in this case, printing the error). But what would happen
|
245
|
-
if she weren't trapping exits? Let's break James apart into two separate
|
246
|
-
objects, one for James himself and one for Little Bastard, his car:
|
247
|
-
|
248
|
-
class PorscheSpider
|
249
|
-
include Celluloid
|
250
|
-
class CarInMyLaneError < StandardError; end
|
251
|
-
|
252
|
-
def drive_on_route_466
|
253
|
-
raise CarInMyLaneError, "head on collision :("
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
class JamesDean
|
258
|
-
include Celluloid
|
259
|
-
|
260
|
-
def initialize
|
261
|
-
@little_bastard = PorscheSpider.new_link
|
262
|
-
end
|
263
|
-
|
264
|
-
def drive_little_bastard
|
265
|
-
@little_bastard.drive_on_route_466
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
If you take a look in JamesDean#initialize, you'll notice that to create an
|
270
|
-
instance of PorcheSpider, James is calling the new_link method.
|
271
|
-
|
272
|
-
This method works similarly to _new_, except it combines _new_ and _link_
|
273
|
-
into a single call.
|
274
|
-
|
275
|
-
Now what happens if we repeat the same scenario with Elizabeth Taylor watching
|
276
|
-
for James Dean's crash?
|
277
|
-
|
278
|
-
>> james = JamesDean.new
|
279
|
-
=> #<Celluloid::Actor(JamesDean:0x1108) @little_bastard=#<Celluloid::Actor(PorscheSpider:0x10ec)>>
|
280
|
-
>> elizabeth = ElizabethTaylor.new
|
281
|
-
=> #<Celluloid::Actor(ElizabethTaylor:0x1144)>
|
282
|
-
>> elizabeth.link james
|
283
|
-
=> #<Celluloid::Actor(JamesDean:0x1108) @little_bastard=#<Celluloid::Actor(PorscheSpider:0x10ec)>>
|
284
|
-
>> james.drive_little_bastard!
|
285
|
-
=> nil
|
286
|
-
Oh no! #<Celluloid::Actor(JamesDean:0x1108) dead> has died because of a PorscheSpider::CarInMyLaneError
|
287
|
-
|
288
|
-
When Little Bastard crashed, it killed James as well. Little Bastard killed
|
289
|
-
James, and because Elizabeth was trapping James' exit events, she received the
|
290
|
-
notification of James' death.
|
291
|
-
|
292
|
-
Actors that are linked together propagate their error messages to all other
|
293
|
-
actors that they're linked to. Unless those actors are trapping exit events,
|
294
|
-
those actors too will die, like James did in this case. If you have many,
|
295
|
-
many actors linked together in a large object graph, killing one will kill them
|
296
|
-
all unless they are trapping exits.
|
297
|
-
|
298
|
-
This allows you to factor your problem into several actors. If an error occurs
|
299
|
-
in any of them, it will kill off all actors used in a particular system. In
|
300
|
-
general, you'll probably want to have a supervisor start a single actor which
|
301
|
-
is in charge of a particular part of your system, and have that actor
|
302
|
-
new_link to other actors which are part of the same system. If any error
|
303
|
-
occurs in any of these actors, all of them will be killed off and the entire
|
304
|
-
subsystem will be restarted by the supervisor in a clean state.
|
305
|
-
|
306
|
-
If, for any reason, you've linked to an actor and want to sever the link,
|
307
|
-
there's a corresponding _unlink_ method to remove links between actors.
|
308
|
-
|
309
|
-
Registry
|
310
|
-
--------
|
311
|
-
|
312
|
-
Celluloid lets you register actors so you can refer to them symbolically.
|
313
|
-
You can register Actors using Celluloid::Actor[]:
|
314
|
-
|
315
|
-
>> james = JamesDean.new
|
316
|
-
=> #<Celluloid::Actor(JamesDean:0x80c27ce0)>
|
317
|
-
>> Celluloid::Actor[:james] = james
|
318
|
-
=> #<Celluloid::Actor(JamesDean:0x80c27ce0)>
|
319
|
-
>> Celluloid::Actor[:james]
|
320
|
-
=> #<Celluloid::Actor(JamesDean:0x80c27ce0)>
|
321
|
-
|
322
|
-
The Celluloid::Actor constant acts as a hash, allowing you to register actors
|
323
|
-
under the name of your choosing, and access actors by name rather than
|
324
|
-
reference. This is important because actors may crash. If you're attempting to
|
325
|
-
reference an actor explicitly by storing it in a variable, you may be holding
|
326
|
-
onto a reference to a crashed copy of that actor, rather than talking to a
|
327
|
-
working, freshly-restarted version.
|
127
|
+
two features of Celluloid called [supervisors](https://github.com/tarcieri/celluloid/wiki/supervisors)
|
128
|
+
and [linking](https://github.com/tarcieri/celluloid/wiki/linking)
|
328
129
|
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
Applications
|
333
|
-
------------
|
334
|
-
|
335
|
-
Celluloid provides a DSL for describing all of the actors in a given
|
336
|
-
application. This lets you start a group of actors in one swoop and
|
337
|
-
also provides an additional level of supervision: applications supervise
|
338
|
-
the supervisors of all the actors in your system, an approach known
|
339
|
-
as supervision trees.
|
340
|
-
|
341
|
-
Define Celluloid::Applications with the following syntax:
|
342
|
-
|
343
|
-
class MyApplication < Celluloid::Application
|
344
|
-
supervise MyActor, :as => :my_actor
|
345
|
-
supervise AnotherActor, :as => :another_actor
|
346
|
-
end
|
347
|
-
|
348
|
-
This will start the MyActor and AnotherActor actors under a supervisor and
|
349
|
-
automatically register them as Celluloid::Actor[:my_actor] and
|
350
|
-
Celluloid::Actor[:another_actor].
|
351
|
-
|
352
|
-
To launch your application, do:
|
353
|
-
|
354
|
-
MyApplication.run
|
355
|
-
|
356
|
-
This launches your application in the foreground. To launch in in the
|
357
|
-
background, do:
|
358
|
-
|
359
|
-
MyApplication.run!
|
360
|
-
|
361
|
-
Signaling
|
362
|
-
---------
|
363
|
-
|
364
|
-
Signaling is an advanced technique similar to condition variables in typical
|
365
|
-
multithreaded programming. One method within a concurrent object can suspend
|
366
|
-
itself waiting for a particular event, allowing other methods to run. Another
|
367
|
-
method can then signal all methods waiting for a particular event, and even
|
368
|
-
send them a value in the process:
|
369
|
-
|
370
|
-
class SignalingExample
|
371
|
-
include Celluloid
|
372
|
-
attr_reader :signaled
|
373
|
-
|
374
|
-
def initialize
|
375
|
-
@signaled = false
|
376
|
-
end
|
377
|
-
|
378
|
-
def wait_for_signal
|
379
|
-
value = wait :ponycopter
|
380
|
-
@signaled = true
|
381
|
-
value
|
382
|
-
end
|
383
|
-
|
384
|
-
def send_signal(value)
|
385
|
-
signal :ponycopter, value
|
386
|
-
end
|
387
|
-
end
|
388
|
-
|
389
|
-
The #wait_for_signal method in turn calls a method called "wait". Wait suspends
|
390
|
-
the running method until another method of the same object calls the "signal"
|
391
|
-
method with the same label.
|
392
|
-
|
393
|
-
The #send_signal method of this class does just that, signaling "ponycopter"
|
394
|
-
with the given value. This value is returned from the original wait call.
|
395
|
-
|
396
|
-
Protocol Interaction
|
397
|
-
--------------------
|
398
|
-
|
399
|
-
The asynchronous message protocol Celluloid uses can be used directly to add
|
400
|
-
new behavior to actors.
|
401
|
-
|
402
|
-
To send a raw asynchronous message to an actor, use:
|
403
|
-
|
404
|
-
actor.mailbox << MyMessage.new
|
405
|
-
|
406
|
-
Methods can wait on incoming MyMessage objects using the #receive method:
|
407
|
-
|
408
|
-
class MyActor
|
409
|
-
def initialize
|
410
|
-
wait_for_my_messages!
|
411
|
-
end
|
412
|
-
|
413
|
-
def wait_for_my_messages
|
414
|
-
loop do
|
415
|
-
message = receive { |msg| msg.is_a? MyMessage }
|
416
|
-
puts "Got a MyMessage: #{message.inspect}"
|
417
|
-
end
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
|
-
The #receive method takes a block, and yields any incoming messages which are
|
422
|
-
received by the current actor to the block, waiting for the block to return
|
423
|
-
true. Calls to #receive sleep until a message is received which makes the
|
424
|
-
block return true, at which point the matching message is returned.
|
425
|
-
|
426
|
-
Handling I/O with Celluloid::IO
|
427
|
-
-------------------------------
|
428
|
-
|
429
|
-
Celluloid provides a separate class of actors which run alongside I/O
|
430
|
-
operations. These actors are slower and more heavyweight and should only be
|
431
|
-
used when writing actors that also handle IO operations. Every IO actor will
|
432
|
-
use 2 file descriptors (it uses a pipe for signaling), so use them sparingly
|
433
|
-
and only when directly interacting with IO.
|
434
|
-
|
435
|
-
To create an IO actor, include Celluloid::IO:
|
436
|
-
|
437
|
-
class IOActor
|
438
|
-
include Celluloid::IO
|
439
|
-
|
440
|
-
def initialize(sock)
|
441
|
-
@sock = sock
|
442
|
-
end
|
443
|
-
|
444
|
-
def read
|
445
|
-
wait_readable(@sock) do
|
446
|
-
@sock.read_nonblock
|
447
|
-
end
|
448
|
-
end
|
449
|
-
end
|
450
|
-
|
451
|
-
The Celluloid::IO#wait_readable and #wait_writeable methods suspend execution
|
452
|
-
of the current method until the given IO object is ready to be read from or
|
453
|
-
written to respectively. In the meantime, the current actor will continue
|
454
|
-
processing incoming messages, allowing it to respond to method requests even
|
455
|
-
while a method (or many methods) are waiting on IO objects.
|
456
|
-
|
457
|
-
Logging
|
458
|
-
-------
|
459
|
-
|
460
|
-
By default, Celluloid will log any errors and backtraces from any crashing
|
461
|
-
actors to STDOUT. However, if you wish you can use any logger which is
|
462
|
-
duck typed with the standard Ruby Logger API (i.e. it implements the #error
|
463
|
-
method). For example, if you're using Celluloid within a Rails
|
464
|
-
application, you'll probably want to do:
|
465
|
-
|
466
|
-
Celluloid.logger = Rails.logger
|
467
|
-
|
468
|
-
The logger class you specify must be thread-safe, although with a logging
|
469
|
-
API about the worst you have to worry about with thread safety bugs is
|
470
|
-
out-of-order messages in the log.
|
471
|
-
|
472
|
-
Implementation and Gotchas
|
473
|
-
--------------------------
|
474
|
-
|
475
|
-
Celluloid is fundamentally a messaging system which uses thread-safe proxies
|
476
|
-
to manage all inter-object communication in the system. While the goal of
|
477
|
-
these proxies is to make it simple for you to write concurrent programs by
|
478
|
-
applying the uniform access principle to thread-safe inter-object messaging,
|
479
|
-
you can't simply forget they're there.
|
480
|
-
|
481
|
-
The thread-safety guarantees Celluloid provides around synchronizing access to
|
482
|
-
instance variables only work so long as all access to actors go through the
|
483
|
-
proxy objects. If the real objects that Celluloid is wrapping in an actor
|
484
|
-
manage to leak out of the system, all hell will break loose.
|
485
|
-
|
486
|
-
Here are a few rules you can follow to keep this from happening:
|
487
|
-
|
488
|
-
1. ***NEVER RETURN SELF*** (or pass self as an argument to other actors): in
|
489
|
-
cases where you want to pass an actor around to other actors or threads,
|
490
|
-
use Celluloid.current_actor, or if you're within an actor itself, you can
|
491
|
-
just call the #current_actor method. If you really need to get ahold of
|
492
|
-
"self" in order to add instance-specific behavior, e.g for metaprogramming
|
493
|
-
purposes or adding stubs during tests, call MyActor#wrapped_object to
|
494
|
-
obtain the actual object an actor is wrapping.
|
495
|
-
|
496
|
-
2. Don't mutate the state of objects you've sent in calls to other actors:
|
497
|
-
This means you must think about data in one of two different ways: either
|
498
|
-
you "fire and forget" the data, leaving it for other actors to do with
|
499
|
-
what they will, or you must treat it as immutable if you have any plans
|
500
|
-
of sharing it with other actors. If you're paranoid (and when you're
|
501
|
-
dealing with concurrency, there's nothing wrong with being paranoid),
|
502
|
-
you can freeze objects so you can detect subsequent mutations (or rather,
|
503
|
-
turn attempts at mutation into errors).
|
504
|
-
|
505
|
-
3. Don't mix Ruby thread primitives and calls to other actors: if you make
|
506
|
-
a call to another actor with a mutex held, you're doing it wrong. It's
|
507
|
-
perfectly fine and strongly encouraged to call out to thread safe
|
508
|
-
libraries from Celluloid actors. However, if you're using libraries that
|
509
|
-
acquire mutexes and then execute callbacks (e.g. they take a block while
|
510
|
-
they're holding a mutex) the guarantees that Celluloid provides will
|
511
|
-
become weak and you may encounter deadlocks.
|
512
|
-
|
513
|
-
4. Use Fibers at your own risk: Celluloid employs Fibers as an intrinsic part
|
514
|
-
of how it implements actors. While it's possible for certain uses of Fibers
|
515
|
-
to cooperatively work alongside how Celluloid behaves, in most cases you'll
|
516
|
-
be writing a check you can't afford. So please ask yourself: why are you
|
517
|
-
using Fibers, and why can't it be solved by a block? If you've got a really
|
518
|
-
good reason and you're feeling lucky, knock yourself out.
|
519
|
-
|
520
|
-
5. If you need to mock the behaviour of an Actor, you should mock its subject
|
521
|
-
rather than the proxy itself (#actor_subject). This ensures that any time
|
522
|
-
the subject calls methods on self, they will also be appropriately mocked.
|
523
|
-
|
524
|
-
On Thread Safety in Ruby
|
525
|
-
------------------------
|
526
|
-
|
527
|
-
Ruby actually has a pretty good story when it comes to thread safety. The best
|
528
|
-
strategy for thread safety is to share as little state as possible, and if
|
529
|
-
you do share state, you should never mutate it. The worry of anyone stepping
|
530
|
-
into a thread safe world is that you're using a bunch of legacy libraries with
|
531
|
-
dubious thread safety. Who knows what those crazy library authors were doing?
|
532
|
-
|
533
|
-
Relax people. You're using a language where somebody can change what the '+'
|
534
|
-
operator does to numbers. So why aren't we afraid to add numbers? Who knows
|
535
|
-
what those crazy library authors may have done! Instead of freaking out, we
|
536
|
-
can learn some telltale signs of things that will cause thread safety problems
|
537
|
-
in Ruby programs so we can identify potential problem libraries just from how
|
538
|
-
their APIs behave.
|
539
|
-
|
540
|
-
The #1 thread safety issue to look out for in a Ruby library is if it provides
|
541
|
-
some sort of singleton access to a particular object through a class method,
|
542
|
-
e.g MyClass.zomgobject, as opposed to asking you do do MyClass.new. If you
|
543
|
-
aren't allocating the object, it isn't yours, it's somebody else's, and you
|
544
|
-
better damn well make sure you can share nice, or you shouldn't play with it
|
545
|
-
at all.
|
546
|
-
|
547
|
-
How do we share nicely? Let's find out by first looking at a thread-unsafe
|
548
|
-
version of a singleton method:
|
549
|
-
|
550
|
-
class Foo
|
551
|
-
def self.current
|
552
|
-
@foo ||= Foo.new
|
553
|
-
end
|
554
|
-
end
|
555
|
-
|
556
|
-
Seems bad. All threads will share access to the same Foo object, and there's
|
557
|
-
also a secondary bug here which means when the object is first being allocated
|
558
|
-
and memoized as @foo. The first thread that tries to allocate it may get a
|
559
|
-
different version than all the other threads because the memo value it set
|
560
|
-
got clobbered by another thread because it's unsynchronized.
|
561
|
-
|
562
|
-
What else can we do? It depends on why the library is memoizing. Perhaps the
|
563
|
-
Foo object has some kind of setup cost, such as making a network connection,
|
564
|
-
and we want to keep it around instead of setting it up and tearing it down
|
565
|
-
every time. If that's the case, the simplest thing we can do to make this
|
566
|
-
code thread safe is to create a thread-specific memo of the object:
|
567
|
-
|
568
|
-
class Foo
|
569
|
-
def self.current
|
570
|
-
Thread.current[:foo] ||= Foo.new
|
571
|
-
end
|
572
|
-
end
|
573
|
-
|
574
|
-
Keep in mind that this will require N Foo objects for N threads. If each
|
575
|
-
object is wrapping a network connection, this might be a concern. That said,
|
576
|
-
if you see this pattern employed in the singleton methods of a library,
|
577
|
-
it's most likely thread safe, provided that Foo doesn't do other wonky things.
|
130
|
+
[Please see the Celluloid Wiki](https://github.com/tarcieri/celluloid/wiki)
|
131
|
+
for additional usage information.
|
578
132
|
|
579
133
|
Contributing to Celluloid
|
580
134
|
-------------------------
|
@@ -583,7 +137,8 @@ Contributing to Celluloid
|
|
583
137
|
* Make your changes and send me a pull request
|
584
138
|
* If I like them I'll merge them and give you commit access to my repository
|
585
139
|
|
586
|
-
|
587
|
-
|
140
|
+
License
|
141
|
+
-------
|
588
142
|
|
589
|
-
Copyright (c) 2011 Tony Arcieri.
|
143
|
+
Copyright (c) 2011 Tony Arcieri. Distributed under the MIT License. See
|
144
|
+
LICENSE.txt for further details.
|