Class: Celluloid::ActorProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/actor_proxy.rb

Overview

A proxy object returned from Celluloid::Actor.spawn/spawn_link which dispatches calls and casts to normal Ruby objects which are running inside of their own threads.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor) ⇒ ActorProxy

Returns a new instance of ActorProxy.



8
9
10
11
12
13
# File 'lib/celluloid/actor_proxy.rb', line 8

def initialize(actor)
  @mailbox, @thread, @klass = actor.mailbox, actor.thread, actor.subject.class.to_s
  
  # Cache "unbanged" versions of methods, e.g. :foobar! => :foobar
  @unbanged_methods = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

method_missing black magic to call bang predicate methods asynchronously



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/celluloid/actor_proxy.rb', line 81

def method_missing(meth, *args, &block)
  # bang methods are async calls
  if meth.match(/!$/)
    # This operation is idempotent and therefore thread-safe
    # The worst case is that the string transformation on the right will
    # run multiple times and make a little more work for the GC
    meth = @unbanged_methods[meth] ||= meth.to_s.sub(/!$/, '').to_sym
    
    Actor.async @mailbox, meth, *args, &block
  else
    Actor.call  @mailbox, meth, *args, &block
  end
end

Instance Attribute Details

#mailboxObject (readonly)

Returns the value of attribute mailbox.



6
7
8
# File 'lib/celluloid/actor_proxy.rb', line 6

def mailbox
  @mailbox
end

Instance Method Details

#_send_(meth, *args, &block) ⇒ Object



15
16
17
# File 'lib/celluloid/actor_proxy.rb', line 15

def _send_(meth, *args, &block)
  Actor.call @mailbox, :__send__, meth, *args, &block
end

#alive?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/celluloid/actor_proxy.rb', line 43

def alive?
  @mailbox.alive?
end

#async(method_name, *args, &block) ⇒ Object

Make an asynchronous call to an actor, for those who don’t like the predicate syntax. TIMTOWTDI!



59
60
61
# File 'lib/celluloid/actor_proxy.rb', line 59

def async(method_name, *args, &block)
  Actor.async @mailbox, method_name, *args, &block
end

#classObject



19
20
21
# File 'lib/celluloid/actor_proxy.rb', line 19

def class
  Actor.call @mailbox, :__send__, :class
end

#future(method_name, *args, &block) ⇒ Object

Create a Celluloid::Future which calls a given method



64
65
66
# File 'lib/celluloid/actor_proxy.rb', line 64

def future(method_name, *args, &block)
  Actor.future @mailbox, method_name, *args, &block
end

#inspectObject



51
52
53
54
55
# File 'lib/celluloid/actor_proxy.rb', line 51

def inspect
  Actor.call @mailbox, :inspect
rescue DeadActorError
  "#<Celluloid::Actor(#{@klass}) dead>"
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/celluloid/actor_proxy.rb', line 27

def is_a?(klass)
  Actor.call @mailbox, :is_a?, klass
end

#kind_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/celluloid/actor_proxy.rb', line 31

def kind_of?(klass)
  Actor.call @mailbox, :kind_of?, klass
end

#methods(include_ancestors = true) ⇒ Object



39
40
41
# File 'lib/celluloid/actor_proxy.rb', line 39

def methods(include_ancestors = true)
  Actor.call @mailbox, :methods, include_ancestors
end

#nameObject



23
24
25
# File 'lib/celluloid/actor_proxy.rb', line 23

def name
  Actor.call @mailbox, :name
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/celluloid/actor_proxy.rb', line 35

def respond_to?(meth)
  Actor.call @mailbox, :respond_to?, meth
end

#terminateObject

Terminate the associated actor



69
70
71
72
# File 'lib/celluloid/actor_proxy.rb', line 69

def terminate
  terminate!
  Thread.pass while alive?
end

#terminate!Object

Terminate the associated actor asynchronously

Raises:



75
76
77
78
# File 'lib/celluloid/actor_proxy.rb', line 75

def terminate!
  raise DeadActorError, "actor already terminated" unless alive?
  @mailbox.system_event TerminationRequest.new
end

#to_sObject



47
48
49
# File 'lib/celluloid/actor_proxy.rb', line 47

def to_s
  Actor.call @mailbox, :to_s
end