Class: Pgbus::ExecutionPools::ThreadPool
- Inherits:
-
Object
- Object
- Pgbus::ExecutionPools::ThreadPool
- Defined in:
- lib/pgbus/execution_pools/thread_pool.rb
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
Instance Method Summary collapse
- #available_capacity ⇒ Object
- #idle? ⇒ Boolean
-
#initialize(capacity:, on_state_change: nil) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #kill ⇒ Object
- #metadata ⇒ Object
- #post(&block) ⇒ Object
-
#quiesced? ⇒ Boolean
True only when every slot is free — all in-flight work has finished.
- #shutdown ⇒ Object
- #wait_for_termination(timeout) ⇒ Object
Constructor Details
#initialize(capacity:, on_state_change: nil) ⇒ ThreadPool
Returns a new instance of ThreadPool.
10 11 12 13 14 15 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 10 def initialize(capacity:, on_state_change: nil) @capacity = capacity @on_state_change = on_state_change @available_capacity = Concurrent::AtomicFixnum.new(capacity) @pool = Concurrent::FixedThreadPool.new(capacity) end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
8 9 10 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 8 def capacity @capacity end |
Instance Method Details
#available_capacity ⇒ Object
32 33 34 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 32 def available_capacity @available_capacity.value end |
#idle? ⇒ Boolean
36 37 38 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 36 def idle? available_capacity.positive? end |
#kill ⇒ Object
55 56 57 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 55 def kill @pool.kill end |
#metadata ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 59 def { mode: :threads, capacity: @capacity, busy: @capacity - available_capacity } end |
#post(&block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 17 def post(&block) @available_capacity.decrement begin @pool.post do block.call ensure @available_capacity.increment @on_state_change&.call end rescue StandardError @available_capacity.increment raise end end |
#quiesced? ⇒ Boolean
True only when every slot is free — all in-flight work has finished. idle? answers "can this pool accept work?"; quiesced? answers "has this pool drained?". The worker drain loop needs the latter.
43 44 45 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 43 def quiesced? available_capacity >= @capacity end |
#shutdown ⇒ Object
47 48 49 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 47 def shutdown @pool.shutdown end |
#wait_for_termination(timeout) ⇒ Object
51 52 53 |
# File 'lib/pgbus/execution_pools/thread_pool.rb', line 51 def wait_for_termination(timeout) @pool.wait_for_termination(timeout) end |