Module: Actorize
- Included in:
- Revactor::HttpFetcher::Worker
- Defined in:
- lib/revactor/actorize.rb
Overview
Any class can be “actorized” by extending it with the Actorize module:
class MyClass
extend Actorize
end
This will create MyClass.spawn and MyClass.spawn_link instance methods. Now you can create instances of your class which run inside Actors:
actor = MyClass.spawn
Actorize defines method_missing for the Actor object, and will delegate any method calls to the Actor to MyClass’s call method. This method should be defined with the following signature:
class MyClass
extend Actorize
def self.call(actor, meth, *args)
...
end
end
The call method receives the actor the call was made on, the method that was invoked, and the arguments that were passed. You can then write a simple RPC mechanism to send a message to the actor and receive a response:
actor << [:call, Actor.current, meth, *args]
Actor.receive do |filter|
filter.when(T[:call_reply, actor]) { |_, _, response| response }
end
Using this approach, you can mix the synchronous approach of Objects with the asynchronous approach of Actors, and effectively duck type Actors to Objects.
Defined Under Namespace
Modules: InstanceMethods
Instance Method Summary collapse
Instance Method Details
#spawn(*args) ⇒ Object
44 45 46 |
# File 'lib/revactor/actorize.rb', line 44 def spawn(*args) _actorize Actor.spawn(*args, &method(:new)) end |
#spawn_link(*args) ⇒ Object
48 49 50 |
# File 'lib/revactor/actorize.rb', line 48 def spawn_link(*args) _actorize Actor.spawn_link(*args, &method(:new)) end |