Skip to content

Commit

Permalink
Better names for thread pool threads (#2657)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierbellone committed Jul 13, 2021
1 parent 1893628 commit 51105f0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/puma/server.rb
Expand Up @@ -227,6 +227,7 @@ def run(background=true, thread_name: 'server')
@status = :run

@thread_pool = ThreadPool.new(
thread_name,
@min_threads,
@max_threads,
::Puma::IOBuffer,
Expand Down
9 changes: 5 additions & 4 deletions lib/puma/thread_pool.rb
Expand Up @@ -29,7 +29,7 @@ class ForceShutdown < RuntimeError
# The block passed is the work that will be performed in each
# thread.
#
def initialize(min, max, *extra, &block)
def initialize(name, min, max, *extra, &block)
@not_empty = ConditionVariable.new
@not_full = ConditionVariable.new
@mutex = Mutex.new
Expand All @@ -39,6 +39,7 @@ def initialize(min, max, *extra, &block)
@spawned = 0
@waiting = 0

@name = name
@min = Integer(min)
@max = Integer(max)
@block = block
Expand Down Expand Up @@ -101,7 +102,7 @@ def spawn_thread
@spawned += 1

th = Thread.new(@spawned) do |spawned|
Puma.set_thread_name 'threadpool %03i' % spawned
Puma.set_thread_name '%s threadpool %03i' % [@name, spawned]
todo = @todo
block = @block
mutex = @mutex
Expand Down Expand Up @@ -319,12 +320,12 @@ def stop
end

def auto_trim!(timeout=30)
@auto_trim = Automaton.new(self, timeout, "threadpool trimmer", :trim)
@auto_trim = Automaton.new(self, timeout, "#{@name} threadpool trimmer", :trim)
@auto_trim.start!
end

def auto_reap!(timeout=5)
@reaper = Automaton.new(self, timeout, "threadpool reaper", :reap)
@reaper = Automaton.new(self, timeout, "#{@name} threadpool reaper", :reap)
@reaper.start!
end

Expand Down
6 changes: 3 additions & 3 deletions test/test_thread_pool.rb
Expand Up @@ -10,12 +10,12 @@ def teardown

def new_pool(min, max, &block)
block = proc { } unless block
@pool = Puma::ThreadPool.new(min, max, &block)
@pool = Puma::ThreadPool.new('test', min, max, &block)
end

def mutex_pool(min, max, &block)
block = proc { } unless block
@pool = MutexPool.new(min, max, &block)
@pool = MutexPool.new('test', min, max, &block)
end

# Wraps ThreadPool work in mutex for better concurrency control.
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_thread_name
thread_name = nil
pool = mutex_pool(0, 1) {thread_name = Thread.current.name}
pool << 1
assert_equal('puma threadpool 001', thread_name)
assert_equal('puma test threadpool 001', thread_name)
end

def test_converts_pool_sizes
Expand Down

0 comments on commit 51105f0

Please sign in to comment.