Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better names for thread pool threads #2657

Merged
merged 1 commit into from Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -318,12 +319,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