Class: LaunchDarkly::Impl::NonBlockingThreadPool Private

Inherits:
Object
  • Object
show all
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.

Since:

  • 5.5.0

Instance Method Summary collapse

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.

Since:

  • 5.5.0



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

#postObject

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.

Since:

  • 5.5.0



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

#shutdownObject

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.

Since:

  • 5.5.0



38
39
40
# File 'lib/ldclient-rb/impl/non_blocking_thread_pool.rb', line 38

def shutdown
  @pool.shutdown
end

#wait_allObject

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.

Since:

  • 5.5.0



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_terminationObject

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.

Since:

  • 5.5.0



42
43
44
# File 'lib/ldclient-rb/impl/non_blocking_thread_pool.rb', line 42

def wait_for_termination
  @pool.wait_for_termination
end