Class: Airbrake::ThreadPool Private
- Inherits:
-
Object
- Object
- Airbrake::ThreadPool
- Includes:
- Loggable
- Defined in:
- lib/airbrake-ruby/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.
ThreadPool implements a simple thread pool that can configure the number of worker threads and the size of the queue to process.
Instance Attribute Summary collapse
-
#workers ⇒ ThreadGroup
readonly
private
The list of workers.
Instance Method Summary collapse
-
#<<(message) ⇒ Boolean
private
Adds a new message to the thread pool.
-
#backlog ⇒ Integer
private
How big the queue is at the moment.
-
#close ⇒ void
private
Closes the thread pool making it a no-op (it shut downs all worker threads).
- #closed? ⇒ Boolean private
-
#has_workers? ⇒ Boolean
private
Checks if a thread pool has any workers.
-
#initialize(worker_size:, queue_size:, block:, name: nil) ⇒ ThreadPool
constructor
private
A new instance of ThreadPool.
- #spawn_workers ⇒ Object private
Methods included from Loggable
Constructor Details
#initialize(worker_size:, queue_size:, block:, name: nil) ⇒ ThreadPool
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 ThreadPool.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 28 def initialize(worker_size:, queue_size:, block:, name: nil) @name = name @worker_size = worker_size @queue_size = queue_size @block = block @queue = SizedQueue.new(queue_size) @workers = ThreadGroup.new @mutex = Mutex.new @pid = nil @closed = false has_workers? end |
Instance Attribute Details
#workers ⇒ ThreadGroup (readonly)
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.
This is exposed for eaiser unit testing
Returns the list of workers.
26 27 28 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 26 def workers @workers end |
Instance Method Details
#<<(message) ⇒ Boolean
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.
Adds a new message to the thread pool. Rejects messages if the queue is at its capacity.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 49 def <<() if backlog >= @queue_size logger.info do "#{LOG_LABEL} ThreadPool has reached its capacity of " \ "#{@queue_size} and the following message will not be " \ "processed: #{.inspect}" end return false end @queue << true end |
#backlog ⇒ Integer
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 how big the queue is at the moment.
64 65 66 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 64 def backlog @queue.size end |
#close ⇒ void
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.
This method returns an undefined value.
Closes the thread pool making it a no-op (it shut downs all worker threads). Before closing, waits on all unprocessed tasks to be processed.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 101 def close threads = @mutex.synchronize do raise Airbrake::Error, 'this thread pool is closed already' if @closed unless @queue.empty? msg = "#{LOG_LABEL} waiting to process #{@queue.size} task(s)..." logger.debug("#{msg} (Ctrl-C to abort)") end @worker_size.times { @queue << :stop } @closed = true @workers.list.dup end threads.each(&:join) logger.debug("#{LOG_LABEL} #{@name} thread pool closed") end |
#closed? ⇒ Boolean
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.
119 120 121 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 119 def closed? @closed end |
#has_workers? ⇒ Boolean
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.
Checks if a thread pool has any workers. A thread pool doesn’t have any workers only in two cases: when it was closed or when all workers crashed. An active thread pool doesn’t have any workers only when something went wrong.
Workers are expected to crash when you fork
the process the workers are living in. In this case we detect a fork
and try to revive them here.
Another possible scenario that crashes workers is when you close the instance on at_exit
, but some other at_exit
hook prevents the process from exiting.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 82 def has_workers? @mutex.synchronize do return false if @closed if @pid != Process.pid && @workers.list.empty? @pid = Process.pid @workers = ThreadGroup.new spawn_workers end !@closed && @workers.list.any? end end |
#spawn_workers ⇒ 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.
123 124 125 |
# File 'lib/airbrake-ruby/thread_pool.rb', line 123 def spawn_workers @worker_size.times { @workers.add(spawn_worker) } end |