Class: Puma::ThreadPool
- Inherits:
-
Object
- Object
- Puma::ThreadPool
- Defined in:
- lib/puma/thread_pool.rb
Overview
A simple thread pool management object.
Defined Under Namespace
Classes: AutoTrim
Instance Attribute Summary collapse
-
#spawned ⇒ Object
readonly
Returns the value of attribute spawned.
-
#trim_requested ⇒ Object
readonly
Returns the value of attribute trim_requested.
Instance Method Summary collapse
-
#<<(work) ⇒ Object
Add
work
to the todo list for a Thread to pickup and process. - #auto_trim!(timeout = 5) ⇒ Object
-
#backlog ⇒ Object
How many objects have yet to be processed by the pool?.
-
#initialize(min, max, &blk) ⇒ ThreadPool
constructor
Maintain a minimum of
min
and maximum ofmax
threads in the pool. -
#shutdown ⇒ Object
Tell all threads in the pool to exit and wait for them to finish.
-
#trim(force = false) ⇒ Object
If too many threads are in the pool, tell one to finish go ahead and exit.
Constructor Details
#initialize(min, max, &blk) ⇒ ThreadPool
Maintain a minimum of min
and maximum of max
threads in the pool.
The block passed is the work that will be performed in each thread.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/puma/thread_pool.rb', line 14 def initialize(min, max, &blk) @cond = ConditionVariable.new @mutex = Mutex.new @todo = [] @spawned = 0 @waiting = 0 @min = min @max = max @block = blk @shutdown = false @trim_requested = 0 @workers = [] @auto_trim = nil @mutex.synchronize do min.times { spawn_thread } end end |
Instance Attribute Details
#spawned ⇒ Object (readonly)
Returns the value of attribute spawned.
40 41 42 |
# File 'lib/puma/thread_pool.rb', line 40 def spawned @spawned end |
#trim_requested ⇒ Object (readonly)
Returns the value of attribute trim_requested.
40 41 42 |
# File 'lib/puma/thread_pool.rb', line 40 def trim_requested @trim_requested end |
Instance Method Details
#<<(work) ⇒ Object
Add work
to the todo list for a Thread to pickup and process.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/puma/thread_pool.rb', line 111 def <<(work) @mutex.synchronize do if @shutdown raise "Unable to add work while shutting down" end @todo << work if @waiting == 0 and @spawned < @max spawn_thread end @cond.signal end end |
#auto_trim!(timeout = 5) ⇒ Object
164 165 166 167 |
# File 'lib/puma/thread_pool.rb', line 164 def auto_trim!(timeout=5) @auto_trim = AutoTrim.new(self, timeout) @auto_trim.start! end |
#backlog ⇒ Object
How many objects have yet to be processed by the pool?
44 45 46 |
# File 'lib/puma/thread_pool.rb', line 44 def backlog @mutex.synchronize { @todo.size } end |
#shutdown ⇒ Object
Tell all threads in the pool to exit and wait for them to finish.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/puma/thread_pool.rb', line 171 def shutdown @mutex.synchronize do @shutdown = true @cond.broadcast @auto_trim.stop if @auto_trim end # Use this instead of #each so that we don't stop in the middle # of each and see a mutated object mid #each @workers.first.join until @workers.empty? @spawned = 0 @workers = [] end |
#trim(force = false) ⇒ Object
If too many threads are in the pool, tell one to finish go ahead and exit. If force
is true, then a trim request is requested even if all threads are being utilized.
131 132 133 134 135 136 137 138 |
# File 'lib/puma/thread_pool.rb', line 131 def trim(force=false) @mutex.synchronize do if (force or @waiting > 0) and @spawned - @trim_requested > @min @trim_requested += 1 @cond.signal end end end |