From 2e12238d09cbe4e69d5b9ba5f2f3690170d384c6 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Tue, 13 Jul 2021 11:58:34 -0700 Subject: [PATCH] Better names for thread pool threads (#2657) --- lib/puma/server.rb | 1 + lib/puma/thread_pool.rb | 9 +++++---- test/test_thread_pool.rb | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index e55e95edd2..8bd2ae63c3 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -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, diff --git a/lib/puma/thread_pool.rb b/lib/puma/thread_pool.rb index abada439fd..f711aee5cd 100644 --- a/lib/puma/thread_pool.rb +++ b/lib/puma/thread_pool.rb @@ -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 @@ -39,6 +39,7 @@ def initialize(min, max, *extra, &block) @spawned = 0 @waiting = 0 + @name = name @min = Integer(min) @max = Integer(max) @block = block @@ -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 @@ -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 diff --git a/test/test_thread_pool.rb b/test/test_thread_pool.rb index b25f34ea67..4166d71bb4 100644 --- a/test/test_thread_pool.rb +++ b/test/test_thread_pool.rb @@ -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. @@ -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