Class: LaunchDarkly::Impl::NonBlockingThreadPool Private
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::NonBlockingThreadPool
- Defined in:
- lib/ldclient-rb/impl/non_blocking_thread_pool.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
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, name = 'LD/NonBlockingThreadPool') ⇒ NonBlockingThreadPool
constructor
private
A new instance of NonBlockingThreadPool.
-
#post ⇒ Object
private
Attempts to submit a job, but only if a worker is available.
- #shutdown ⇒ Object private
-
#wait_all ⇒ Object
private
Waits until no jobs are executing, without shutting down the pool.
- #wait_for_termination ⇒ Object private
Constructor Details
#initialize(capacity, name = 'LD/NonBlockingThreadPool') ⇒ NonBlockingThreadPool
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of NonBlockingThreadPool.
11 12 13 14 15 |
# File 'lib/ldclient-rb/impl/non_blocking_thread_pool.rb', line 11 def initialize(capacity, name = 'LD/NonBlockingThreadPool') @capacity = capacity @pool = Concurrent::FixedThreadPool.new(capacity, name: name) @semaphore = Concurrent::Semaphore.new(capacity) end |
Instance Method Details
#post ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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/impl/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
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
38 39 40 |
# File 'lib/ldclient-rb/impl/non_blocking_thread_pool.rb', line 38 def shutdown @pool.shutdown end |
#wait_all ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Waits until no jobs are executing, without shutting down the pool.
33 34 35 36 |
# File 'lib/ldclient-rb/impl/non_blocking_thread_pool.rb', line 33 def wait_all @semaphore.acquire(@capacity) @semaphore.release(@capacity) end |
#wait_for_termination ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
42 43 44 |
# File 'lib/ldclient-rb/impl/non_blocking_thread_pool.rb', line 42 def wait_for_termination @pool.wait_for_termination end |