Object
Manages a fixed-size pool of workers Delegates work (i.e. methods) and supervises workers Don't use this class directly. Instead use MyKlass.pool
# File lib/celluloid/pool_manager.rb, line 12 def initialize(worker_class, options = {}) @size = options[:size] || [Celluloid.cores, 2].max raise ArgumentError, "minimum pool size is 2" if @size < 2 @worker_class = worker_class @args = options[:args] ? Array(options[:args]) : [] @idle = @size.times.map { worker_class.new_link(*@args) } # FIXME: Another data structure (e.g. Set) would be more appropriate # here except it causes MRI to crash :o @busy = [] end
Spawn a new worker for every crashed one
# File lib/celluloid/pool_manager.rb, line 107 def __crash_handler__(actor, reason) @busy.delete actor @idle.delete actor return unless reason @idle << @worker_class.new_link(*@args) signal :respawn_complete end
Provision a new worker
# File lib/celluloid/pool_manager.rb, line 93 def __provision_worker__ while @idle.empty? # Wait for responses from one of the busy workers response = exclusive { receive { |msg| msg.is_a?(Response) } } Thread.current[:celluloid_actor].handle_message(response) end worker = @idle.shift @busy << worker worker end
# File lib/celluloid/pool_manager.rb, line 26 def __shutdown__ terminators = (@idle + @busy).each do |actor| begin actor.future(:terminate) rescue DeadActorError, MailboxError end end terminators.compact.each { |terminator| terminator.value rescue nil } end
# File lib/celluloid/pool_manager.rb, line 37 def _send_(method, *args, &block) worker = __provision_worker__ begin worker._send_ method, *args, &block rescue DeadActorError # if we get a dead actor out of the pool wait :respawn_complete worker = __provision_worker__ retry rescue Exception => ex abort ex ensure if worker.alive? @idle << worker @busy.delete worker end end end
# File lib/celluloid/pool_manager.rb, line 84 def busy_size @busy.length end
# File lib/celluloid/pool_manager.rb, line 88 def idle_size @idle.length end
# File lib/celluloid/pool_manager.rb, line 76 def inspect _send_ :inspect end
# File lib/celluloid/pool_manager.rb, line 60 def is_a?(klass) _send_ :is_a?, klass end
# File lib/celluloid/pool_manager.rb, line 64 def kind_of?(klass) _send_ :kind_of?, klass end
# File lib/celluloid/pool_manager.rb, line 120 def method_missing(method, *args, &block) if respond_to?(method) _send_ method, *args, &block else super end end
# File lib/celluloid/pool_manager.rb, line 68 def methods(include_ancestors = true) _send_ :methods, include_ancestors end
# File lib/celluloid/pool_manager.rb, line 56 def name _send_ @mailbox, :name end
Generated with the Darkfish Rdoc Generator 2.