Class: Celluloid::Group::Pool

Inherits:
Celluloid::Group show all
Defined in:
lib/celluloid/group/pool.rb

Instance Attribute Summary collapse

Attributes inherited from Celluloid::Group

#group

Instance Method Summary collapse

Methods inherited from Celluloid::Group

#active?, #assert_active, #assert_inactive, #each, #each_actor, #forked?, #purge, #to_a

Constructor Details

#initializePool

Returns a new instance of Pool.


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/celluloid/group/pool.rb', line 11

def initialize
  super
  @mutex = Mutex.new
  @idle_threads = []
  @group  = []
  @busy_size = 0
  @idle_size = 0

  # TODO: should really adjust this based on usage
  @max_idle = 16
end

Instance Attribute Details

#busy_sizeObject (readonly)

Returns the value of attribute busy_size


31
32
33
# File 'lib/celluloid/group/pool.rb', line 31

def busy_size
  @busy_size
end

#idle_sizeObject (readonly)

Returns the value of attribute idle_size


33
34
35
# File 'lib/celluloid/group/pool.rb', line 33

def idle_size
  @idle_size
end

#max_idleObject

You do not want to use this. Truly, you do not. There is no scenario when you will. But. If you somehow do.. `Celluloid.group_class = Celluloid::Group::Pool` and weep.


9
10
11
# File 'lib/celluloid/group/pool.rb', line 9

def max_idle
  @max_idle
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/celluloid/group/pool.rb', line 27

def busy?
  busy_size.count > 0
end

#get(&block) ⇒ Object

Get a thread from the pool, running the given block


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/celluloid/group/pool.rb', line 36

def get(&block)
  @mutex.synchronize do
    assert_active

    begin
      if @idle_threads.empty?
        thread = create
      else
        thread = @idle_threads.pop
        @idle_size = @idle_threads.length
      end
    end until thread.status # handle crashed threads

    thread.busy = true
    @busy_size += 1
    thread[:celluloid_queue] << block
    thread
  end
end

#idle?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/celluloid/group/pool.rb', line 23

def idle?
  busy_size.count == 0
end

#put(thread) ⇒ Object

Return a thread to the pool


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/celluloid/group/pool.rb', line 57

def put(thread)
  @mutex.synchronize do
    thread.busy = false
    if idle_size + 1 >= @max_idle
      thread[:celluloid_queue] << nil
      @busy_size -= 1
      @group.delete(thread)
    else
      @idle_threads.push thread
      @busy_size -= 1
      @idle_size = @idle_threads.length
      clean_thread_locals(thread)
    end
  end
end

#shutdownObject


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/celluloid/group/pool.rb', line 73

def shutdown
  @running = false
  @mutex.synchronize do
    finalize
    @group.each do |thread|
      thread[:celluloid_queue] << nil
    end
    @group.shift.kill until @group.empty?
    @idle_threads.clear
    @busy_size = 0
    @idle_size = 0
  end
end