class LaunchDarkly::NonBlockingThreadPool

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. @private

Public Class Methods

new(capacity) click to toggle source
# 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

Public Instance Methods

post() { || ... } click to toggle source

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.

# File lib/ldclient-rb/non_blocking_thread_pool.rb, line 19
def post
  if !@semaphore.try_acquire(1)
    return
  end
  @pool.post do
    begin
      yield
    ensure
      @semaphore.release(1)
    end
  end
end
shutdown() click to toggle source
# File lib/ldclient-rb/non_blocking_thread_pool.rb, line 38
def shutdown
  @pool.shutdown
end
wait_all() click to toggle source

Waits until no jobs are executing, without shutting down the pool.

# File lib/ldclient-rb/non_blocking_thread_pool.rb, line 33
def wait_all
  @semaphore.acquire(@capacity)
  @semaphore.release(@capacity)
end
wait_for_termination() click to toggle source
# File lib/ldclient-rb/non_blocking_thread_pool.rb, line 42
def wait_for_termination
  @pool.wait_for_termination
end