Class: Celluloid::Pool

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/celluloid/pool.rb

Overview

DEPRECATED: please use Celluloid::Worker instead

Constant Summary

Constants included from Celluloid

SHUTDOWN_TIMEOUT, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Celluloid

#abort, actor?, #after, #alive?, #async, cores, current_actor, #current_actor, #defer, #every, exception_handler, #exclusive, exclusive?, #future, included, #inspect, #link, #linked_to?, #links, #method_missing, #name, #notify_link, #notify_unlink, #receive, receive, shutdown, #signal, #sleep, sleep, #tasks, #terminate, #unlink, uuid, version, #wait, #wrapped_object

Constructor Details

#initialize(klass, options = {}) ⇒ Pool

Takes a class of actor to pool and a hash of options:

  • initial_size: how many actors to eagerly create

  • max_size: maximum number of actors (default one actor per CPU core)

  • args: an array of arguments to pass to the actor’s initialize

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/celluloid/pool.rb', line 13

def initialize(klass, options = {})
  raise ArgumentError, "A Pool has a minimum size of 2" if options[:max_size] && options[:max_size] < 2
  opts = {
    :initial_size => 1,
    :max_size     => [Celluloid.cores, 2].max,
    :args         => []
  }.merge(options)

  @klass, @args = klass, opts[:args]
  @max_actors = opts[:max_size]
  @idle_actors, @running_actors = 0, 0
  @actors = []

  opts[:initial_size].times do
    @actors << spawn
    @idle_actors += 1
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Celluloid

Instance Attribute Details

#max_actorsObject (readonly)

Returns the value of attribute max_actors.



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

def max_actors
  @max_actors
end

Instance Method Details

#crash_handler(actor, reason) ⇒ Object

Handle crashed actors



90
91
92
93
94
95
96
# File 'lib/celluloid/pool.rb', line 90

def crash_handler(actor, reason)
  @idle_actors    -= 1 if @actors.delete actor
  @running_actors -= 1

  # If we were maxed out before...
  signal :ready if @max_actors and @running_actors + 1 == @max_actors
end

#getObject

Get an actor from the pool. Actors taken from the pool must be put back with Pool#put. Alternatively, you can use get with a block form:

pool.get { |actor| ... }

This will automatically return actors to the pool when the block completes



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
# File 'lib/celluloid/pool.rb', line 38

def get
  if @max_actors and @running_actors == @max_actors
    wait :ready
  end

  actor = @actors.shift
  if actor
    @idle_actors -= 1
  else
    actor = spawn
  end

  if block_given?
    begin
      yield actor
    rescue => ex
    end

    put actor
    abort ex if ex
    nil
  else
    actor
  end
end

#idle_countObject Also known as: idle_size

Number of idle actors in the pool



84
85
86
# File 'lib/celluloid/pool.rb', line 84

def idle_count
  @idle_actors
end

#put(actor) ⇒ Object

Return an actor to the pool



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/celluloid/pool.rb', line 65

def put(actor)
  begin
    raise TypeError, "expecting a #{@klass} actor" unless actor.is_a? @klass
  rescue DeadActorError
    # The actor may have died before it was handed back to us
    # We'll let the crash_handler deal with it in due time
    return
  end

  @actors << actor
  @idle_actors += 1
end

#sizeObject

Number of active actors in this pool



79
80
81
# File 'lib/celluloid/pool.rb', line 79

def size
  @running_actors
end

#spawnObject

Spawn an actor of the given class



99
100
101
102
103
# File 'lib/celluloid/pool.rb', line 99

def spawn
  worker = @klass.new_link(*@args)
  @running_actors += 1
  worker
end