Class: TunnelRb::Server::ThreadPool
- Inherits:
-
Object
- Object
- TunnelRb::Server::ThreadPool
- Defined in:
- lib/tunnel_rb/server/thread_pool.rb
Overview
Bounded worker pool. ‘submit` blocks the caller when the queue is full, which propagates backpressure all the way to TCP accept loops.
Constant Summary collapse
- SHUTDOWN =
:shutdown
Instance Method Summary collapse
-
#initialize(size, max_queue: size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #shutdown ⇒ Object
- #submit(&block) ⇒ Object
Constructor Details
#initialize(size, max_queue: size) ⇒ ThreadPool
Returns a new instance of ThreadPool.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tunnel_rb/server/thread_pool.rb', line 12 def initialize(size, max_queue: size) @size = size @queue = SizedQueue.new(max_queue) @workers = Array.new(size) do Thread.new do loop do job = @queue.pop break if job == SHUTDOWN job.call rescue => e warn "ThreadPool job error: #{e.}" end end end end |
Instance Method Details
#shutdown ⇒ Object
33 34 35 36 |
# File 'lib/tunnel_rb/server/thread_pool.rb', line 33 def shutdown @size.times { @queue << SHUTDOWN } @workers.each { |t| t.join(2) } end |
#submit(&block) ⇒ Object
29 30 31 |
# File 'lib/tunnel_rb/server/thread_pool.rb', line 29 def submit(&block) @queue << block end |