1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
require "timers"
module Celluloid
# Actors are Celluloid's concurrency primitive. They're implemented as
# normal Ruby objects wrapped in threads which communicate with asynchronous
# messages.
class Actor
attr_reader :behavior, :proxy, :tasks, :links, :mailbox, :thread, :name, :timers
attr_writer :exit_handler
class << self
extend Forwardable
def_delegators :"Celluloid.actor_system", :[], :[]=, :delete, :registered, :clear_registry
# Obtain the current actor
def current
actor = Thread.current[:celluloid_actor]
raise NotActorError, "not in actor scope" unless actor
actor.behavior_proxy
end
# Obtain the name of the current actor
def registered_name
actor = Thread.current[:celluloid_actor]
raise NotActorError, "not in actor scope" unless actor
actor.name
end
# Invoke a method on the given actor via its mailbox
def call(mailbox, meth, *args, &block)
proxy = Proxy::Sync.new(mailbox, "UnknownClass")
proxy.method_missing(meth, *args, &block)
end
# Invoke a method asynchronously on an actor via its mailbox
def async(mailbox, meth, *args, &block)
proxy = Proxy::Async.new(mailbox, "UnknownClass")
proxy.method_missing(meth, *args, &block)
end
# Call a method asynchronously and retrieve its value later
def future(mailbox, meth, *args, &block)
proxy = Proxy::Future.new(mailbox, "UnknownClass")
proxy.method_missing(meth, *args, &block)
end
# Obtain all running actors in the system
def all
Celluloid.actor_system.running
end
# Watch for exit events from another actor
def monitor(actor)
raise NotActorError, "can't link outside actor context" unless Celluloid.actor?
Thread.current[:celluloid_actor].linking_request(actor, :link)
end
# Stop waiting for exit events from another actor
def unmonitor(actor)
raise NotActorError, "can't link outside actor context" unless Celluloid.actor?
Thread.current[:celluloid_actor].linking_request(actor, :unlink)
end
# Link to another actor
def link(actor)
monitor actor
Thread.current[:celluloid_actor].links << actor
end
# Unlink from another actor
def unlink(actor)
unmonitor actor
Thread.current[:celluloid_actor].links.delete actor
end
# Are we monitoring the given actor?
def monitoring?(actor)
actor.links.include? Actor.current
end
# Are we bidirectionally linked to the given actor?
def linked_to?(actor)
monitoring?(actor) && Thread.current[:celluloid_actor].links.include?(actor)
end
unless RUBY_PLATFORM == "java" || RUBY_ENGINE == "rbx"
# Forcibly kill a given actor
def kill(actor)
actor.thread.kill
actor.mailbox.shutdown if actor.mailbox.alive?
end
end
# Wait for an actor to terminate
def join(actor, timeout = nil)
actor.thread.join(timeout)
actor
end
end
def initialize(behavior, options)
@behavior = behavior
@actor_system = options.fetch(:actor_system)
@mailbox = options.fetch(:mailbox_class, Mailbox).new
@mailbox.max_size = options.fetch(:mailbox_size, nil)
@task_class = options[:task_class] || Celluloid.task_class
@exit_handler = method(:default_exit_handler)
@exclusive = options.fetch(:exclusive, false)
@timers = Timers::Group.new
@tasks = Internals::TaskSet.new
@links = Internals::Links.new
@handlers = Internals::Handlers.new
@receivers = Internals::Receivers.new(@timers)
@signals = Internals::Signals.new
@running = false
@name = nil
handle(SystemEvent) do |message|
handle_system_event message
end
end
def start
@running = true
@thread = Internals::ThreadHandle.new(@actor_system, :actor) do
setup_thread
run
end
@proxy = Proxy::Actor.new(@mailbox, @thread)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
Celluloid::Probe.actor_created(self) if $CELLULOID_MONITORING
# rubocop:enable Style/GlobalVars
end
def behavior_proxy
@behavior.proxy
end
def setup_thread
Thread.current[:celluloid_actor] = self
Thread.current[:celluloid_mailbox] = @mailbox
end
# Run the actor loop
def run
while @running
begin
@timers.wait do |interval|
interval = 0 if interval && interval < 0
if message = @mailbox.check(interval)
handle_message(message)
break unless @running
end
end
rescue MailboxShutdown
@running = false
rescue MailboxDead
# TODO: not tests (but fails occasionally in tests)
@running = false
end
end
shutdown
rescue ::Exception => ex
handle_crash(ex)
raise unless ex.is_a?(StandardError) || ex.is_a?(Celluloid::Interruption)
end
# Terminate this actor
def terminate
@running = false
end
# Perform a linking request with another actor
def linking_request(receiver, type)
Celluloid.exclusive do
receiver.mailbox << LinkingRequest.new(Actor.current, type)
system_events = []
Timers::Wait.for(LINKING_TIMEOUT) do |remaining|
begin
message = @mailbox.receive(remaining) do |msg|
msg.is_a?(LinkingResponse) &&
msg.actor.mailbox.address == receiver.mailbox.address &&
msg.type == type
end
rescue TaskTimeout
next # IO reactor did something, no message in queue yet.
end
if message.instance_of? LinkingResponse
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
Celluloid::Probe.actors_linked(self, receiver) if $CELLULOID_MONITORING
# rubocop:enable Style/GlobalVars
system_events.each { |ev| @mailbox << ev }
return
elsif message.is_a? SystemEvent
# Queue up pending system events to be processed after we've successfully linked
system_events << message
else raise "Unexpected message type: #{message.class}. Expected LinkingResponse, NilClass, SystemEvent."
end
end
raise TaskTimeout, "linking timeout of #{LINKING_TIMEOUT} seconds exceeded with receiver: #{receiver}"
end
end
# Send a signal with the given name to all waiting methods
def signal(name, value = nil)
@signals.broadcast name, value
end
# Wait for the given signal
def wait(name)
@signals.wait name
end
# Register a new handler for a given pattern
def handle(*patterns, &block)
@handlers.handle(*patterns, &block)
end
# Receive an asynchronous message
def receive(timeout = nil, &block)
loop do
message = @receivers.receive(timeout, &block)
return message unless message.is_a?(SystemEvent)
handle_system_event(message)
end
end
# Schedule a block to run at the given time
def after(interval, &block)
@timers.after(interval) { task(:timer, &block) }
end
# Schedule a block to run at the given time
def every(interval, &block)
@timers.every(interval) { task(:timer, &block) }
end
def timeout(duration)
bt = caller
task = Task.current
timer = @timers.after(duration) do
exception = TaskTimeout.new("execution expired")
exception.set_backtrace bt
task.resume exception
end
yield
ensure
timer.cancel if timer
end
class Sleeper
def initialize(timers, interval)
@timers = timers
@interval = interval
end
def before_suspend(task)
@timers.after(@interval) { task.resume }
end
def wait
Kernel.sleep(@interval)
end
end
# Sleep for the given amount of time
def sleep(interval)
sleeper = Sleeper.new(@timers, interval)
Celluloid.suspend(:sleeping, sleeper)
end
# Handle standard low-priority messages
def handle_message(message)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Metrics/LineLength, Style/GlobalVars
Internals::Logger.debug "Discarded message (unhandled): #{message}" if !@handlers.handle_message(message) && !@receivers.handle_message(message) && $CELLULOID_DEBUG
# rubocop:enable Metrics/LineLength, Style/GlobalVars
message
end
def default_exit_handler(event)
raise event.reason if event.reason
end
# Handle any exceptions that occur within a running actor
def handle_crash(exception)
# TODO: add meta info
Internals::Logger.crash("Actor crashed!", exception)
shutdown ExitEvent.new(behavior_proxy, exception)
rescue => ex
Internals::Logger.crash("Actor#handle_crash CRASHED!", ex)
end
# Handle cleaning up this actor after it exits
def shutdown(exit_event = ExitEvent.new(behavior_proxy))
@behavior.shutdown
cleanup exit_event
ensure
Thread.current[:celluloid_actor] = nil
Thread.current[:celluloid_mailbox] = nil
end
# Clean up after this actor
def cleanup(exit_event)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
Celluloid::Probe.actor_died(self) if $CELLULOID_MONITORING
# rubocop:enable Style/GlobalVars
@mailbox.shutdown
@links.each do |actor|
actor.mailbox << exit_event if actor.mailbox.alive?
end
tasks.to_a.each do |task|
begin
task.terminate
rescue DeadTaskError
# TODO: not tested (failed on Travis)
end
end
rescue => ex
# TODO: metadata
Internals::Logger.crash("CLEANUP CRASHED!", ex)
end
# Run a method inside a task unless it's exclusive
def task(task_type, meta = nil)
@task_class.new(task_type, meta) do
if @exclusive
Celluloid.exclusive { yield }
else
yield
end
end.resume
end
end
end
|