Class: Celluloid::PoolManager

### Home Classes Methods

### In Files

- lib/celluloid/pool_manager.rb

### Parent

Object

### Methods

- ::new
- #__crash_handler__
- #__provision_worker__
- #__shutdown__
- #_send_
- #busy_size
- #idle_size
- #inspect
- #is_a?
- #kind_of?
- #method_missing
- #methods
- #name
- #respond_to?
- #size
- #size=
- #to_s

### Included Modules

- Celluloid

### Class/Module Index

Quicksearch

- Celluloid
- Celluloid::AbortError
- Celluloid::AbstractProxy
- Celluloid::Actor
- Celluloid::Actor::Sleeper
- Celluloid::ActorProxy
- Celluloid::ActorSystem
- Celluloid::AsyncCall
- Celluloid::AsyncProxy
- Celluloid::BlockCall
- Celluloid::BlockProxy
- Celluloid::BlockResponse
- Celluloid::CPUCounter
- Celluloid::Call
- Celluloid::CallChain
- Celluloid::Cell
- Celluloid::Cell::ExitHandler
- Celluloid::CellProxy
- Celluloid::ClassMethods
- Celluloid::Condition
- Celluloid::Condition::Waiter
- Celluloid::ConditionError
- Celluloid::DeadTaskError
- Celluloid::ErrorResponse
- Celluloid::EventedMailbox
- Celluloid::ExitEvent
- Celluloid::FSM
- Celluloid::FSM::ClassMethods
- Celluloid::FSM::State
- Celluloid::FSM::UnattachedError
- Celluloid::FiberStackError
- Celluloid::Future
- Celluloid::Future::Result
- Celluloid::FutureProxy
- Celluloid::Handler
- Celluloid::Handlers
- Celluloid::Incident
- Celluloid::IncidentLogger
- Celluloid::IncidentLogger::Severity
- Celluloid::IncidentReporter
- Celluloid::IncidentReporter::Formatter
- Celluloid::InstanceMethods
- Celluloid::InternalPool
- Celluloid::LinkingRequest
- Celluloid::LinkingResponse
- Celluloid::Links
- Celluloid::LogEvent
- Celluloid::Logger
- Celluloid::Logger::WithBacktrace
- Celluloid::Mailbox
- Celluloid::MailboxDead
- Celluloid::MailboxShutdown
- Celluloid::Method
- Celluloid::NamingRequest
- Celluloid::NotTaskError
- Celluloid::Notifications
- Celluloid::Notifications::Fanout
- Celluloid::Notifications::Subscriber
- Celluloid::PoolManager
- Celluloid::Probe
- Celluloid::Properties
- Celluloid::Receiver
- Celluloid::Receivers
- Celluloid::Registry
- Celluloid::Response
- Celluloid::ResumableError
- Celluloid::RingBuffer
- Celluloid::SignalConditionRequest
- Celluloid::Signals
- Celluloid::StackDump
- Celluloid::StackDump::ActorState
- Celluloid::StackDump::CellState
- Celluloid::StackDump::DisplayBacktrace
- Celluloid::StackDump::TaskState
- Celluloid::StackDump::ThreadState
- Celluloid::SuccessResponse
- Celluloid::SupervisionGroup
- Celluloid::SupervisionGroup::Member
- Celluloid::Supervisor
- Celluloid::SyncCall
- Celluloid::SyncProxy
- Celluloid::SystemEvent
- Celluloid::Task
- Celluloid::Task::TerminatedError
- Celluloid::Task::TimeoutError
- Celluloid::TaskFiber
- Celluloid::TaskSet
- Celluloid::TaskThread
- Celluloid::TerminationRequest
- Celluloid::Thread
- Celluloid::ThreadHandle
- Celluloid::UUID
- org
- org::jruby
- org::jruby::ext
- org::jruby::ext::fiber
- org::jruby::ext::fiber::ThreadFiber
- Bar
- Baz
- Blocker
- ExampleActorClass
- ExampleCrash
- ExampleMailbox
- ExampleProxy
- ExampleTask
- Fiber
- Foo
- MockActor
- Object
- Thread

No matching classes.

# Celluloid::PoolManager

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

### Public Class Methods

new(worker_class, options = {}) click to toggle source

    # File lib/celluloid/pool_manager.rb, line 12
    def initialize(worker_class, options = {})
      @size = options[:size] || [Celluloid.cores || 2, 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

### Public Instance Methods

__crash_handler__(actor, reason) click to toggle source

Spawn a new worker for every crashed one

    # File lib/celluloid/pool_manager.rb, line 125
    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_worker__() click to toggle source

Provision a new worker

    # File lib/celluloid/pool_manager.rb, line 110
    def __provision_worker__
      Task.current.guard_warnings = true
      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

__shutdown__() click to toggle source

    # File lib/celluloid/pool_manager.rb, line 26
    def __shutdown__
      terminators = (@idle + @busy).map do |actor|
        begin
          actor.future(:terminate)
        rescue DeadActorError
        end
      end

      terminators.compact.each { |terminator| terminator.value rescue nil }
    end

_send_(method, *args, &block) click to toggle source

    # 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

busy_size() click to toggle source

    # File lib/celluloid/pool_manager.rb, line 101
    def busy_size
      @busy.length
    end

idle_size() click to toggle source

    # File lib/celluloid/pool_manager.rb, line 105
    def idle_size
      @idle.length
    end

inspect() click to toggle source

    # File lib/celluloid/pool_manager.rb, line 76
    def inspect
      _send_ :inspect
    end

is_a?(klass) click to toggle source

    # File lib/celluloid/pool_manager.rb, line 60
    def is_a?(klass)
      _send_ :is_a?, klass
    end

kind_of?(klass) click to toggle source

    # File lib/celluloid/pool_manager.rb, line 64
    def kind_of?(klass)
      _send_ :kind_of?, klass
    end

method_missing(method, *args, &block) click to toggle source

    # File lib/celluloid/pool_manager.rb, line 138
    def method_missing(method, *args, &block)
      if respond_to?(method)
        _send_ method, *args, &block
      else
        super
      end
    end

methods(include_ancestors = true) click to toggle source

    # File lib/celluloid/pool_manager.rb, line 68
    def methods(include_ancestors = true)
      _send_ :methods, include_ancestors
    end

name() click to toggle source

    # File lib/celluloid/pool_manager.rb, line 56
    def name
      _send_ @mailbox, :name
    end

respond_to?(method, include_private = false) click to toggle source

    # File lib/celluloid/pool_manager.rb, line 134
    def respond_to?(method, include_private = false)
      super || @worker_class.instance_methods.include?(method.to_sym)
    end

size() click to toggle source

    # File lib/celluloid/pool_manager.rb, line 80
    def size
      @size
    end

size=(new_size) click to toggle source

    # File lib/celluloid/pool_manager.rb, line 84
    def size=(new_size)
      new_size = [0, new_size].max

      if new_size > size
        delta = new_size - size
        delta.times { @idle << @worker_class.new_link(*@args) }
      else
        (size - new_size).times do
          worker = __provision_worker__
          unlink worker
          @busy.delete worker
          worker.terminate
        end
      end
      @size = new_size
    end

to_s() click to toggle source

    # File lib/celluloid/pool_manager.rb, line 72
    def to_s
      _send_ :to_s
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.