Class: LaunchDarkly::NonBlockingThreadPool
- Inherits:
-
Object
- Object
- LaunchDarkly::NonBlockingThreadPool
- Defined in:
- lib/ldclient-rb/non_blocking_thread_pool.rb
Overview
Simple wrapper for a FixedThreadPool that rejects new jobs if all the threads are busy, rather than blocking. Also provides a way to wait for all jobs to finish without shutting down.
Instance Method Summary collapse
-
#initialize(capacity) ⇒ NonBlockingThreadPool
constructor
A new instance of NonBlockingThreadPool.
-
#post ⇒ Object
Attempts to submit a job, but only if a worker is available.
- #shutdown ⇒ Object
-
#wait_all ⇒ Object
Waits until no jobs are executing, without shutting down the pool.
- #wait_for_termination ⇒ Object
Constructor Details
#initialize(capacity) ⇒ NonBlockingThreadPool
Returns a new instance of NonBlockingThreadPool.
11 12 13 14 15 |
# File 'lib/ldclient-rb/non_blocking_thread_pool.rb', line 11 def initialize(capacity) @capacity = capacity @pool = Concurrent::FixedThreadPool.new(capacity) @semaphore = Concurrent::Semaphore.new(capacity) end |
Instance Method Details
#post ⇒ Object
Attempts to submit a job, but only if a worker is available. Unlike the regular post method, this returns a value: true if the job was submitted, false if all workers are busy.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ldclient-rb/non_blocking_thread_pool.rb', line 19 def post unless @semaphore.try_acquire(1) return end @pool.post do begin yield ensure @semaphore.release(1) end end end |
#shutdown ⇒ Object
38 39 40 |
# File 'lib/ldclient-rb/non_blocking_thread_pool.rb', line 38 def shutdown @pool.shutdown end |
#wait_all ⇒ Object
Waits until no jobs are executing, without shutting down the pool.
33 34 35 36 |
# File 'lib/ldclient-rb/non_blocking_thread_pool.rb', line 33 def wait_all @semaphore.acquire(@capacity) @semaphore.release(@capacity) end |
#wait_for_termination ⇒ Object
42 43 44 |
# File 'lib/ldclient-rb/non_blocking_thread_pool.rb', line 42 def wait_for_termination @pool.wait_for_termination end |