Class: Celluloid::Group::Pool
- Inherits:
-
Celluloid::Group
- Object
- Celluloid::Group
- Celluloid::Group::Pool
- Defined in:
- lib/celluloid/group/pool.rb
Instance Attribute Summary collapse
-
#busy_size ⇒ Object
readonly
Returns the value of attribute busy_size.
-
#idle_size ⇒ Object
readonly
Returns the value of attribute idle_size.
-
#max_idle ⇒ Object
You do not want to use this.
Attributes inherited from Celluloid::Group
Instance Method Summary collapse
- #busy? ⇒ Boolean
-
#get(&block) ⇒ Object
Get a thread from the pool, running the given block.
- #idle? ⇒ Boolean
-
#initialize ⇒ Pool
constructor
A new instance of Pool.
-
#put(thread) ⇒ Object
Return a thread to the pool.
- #shutdown ⇒ Object
Methods inherited from Celluloid::Group
#active?, #assert_active, #assert_inactive, #each, #each_actor, #forked?, #purge, #to_a
Constructor Details
#initialize ⇒ Pool
Returns a new instance of Pool.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/celluloid/group/pool.rb', line 9 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_size ⇒ Object (readonly)
Returns the value of attribute busy_size.
29 30 31 |
# File 'lib/celluloid/group/pool.rb', line 29 def busy_size @busy_size end |
#idle_size ⇒ Object (readonly)
Returns the value of attribute idle_size.
31 32 33 |
# File 'lib/celluloid/group/pool.rb', line 31 def idle_size @idle_size end |
#max_idle ⇒ Object
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.
7 8 9 |
# File 'lib/celluloid/group/pool.rb', line 7 def max_idle @max_idle end |
Instance Method Details
#busy? ⇒ Boolean
25 26 27 |
# File 'lib/celluloid/group/pool.rb', line 25 def busy? busy_size.count > 0 end |
#get(&block) ⇒ Object
Get a thread from the pool, running the given block
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/celluloid/group/pool.rb', line 34 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
21 22 23 |
# File 'lib/celluloid/group/pool.rb', line 21 def idle? busy_size.count == 0 end |
#put(thread) ⇒ Object
Return a thread to the pool
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/celluloid/group/pool.rb', line 55 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 |
#shutdown ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/celluloid/group/pool.rb', line 71 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 |