Skip to content

Commit

Permalink
Avoid spawning Threadpool#trim thread if pool size is fixed (#3384)
Browse files Browse the repository at this point in the history
Co-authored-by: ouyangjinting <OuYangJinTing@users.noreply.github.com>
  • Loading branch information
joshuay03 and OuYangJinTing authored Nov 23, 2024
1 parent 8c4e80d commit 5f41574
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/puma/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,8 @@ def run(background=true, thread_name: 'srv')
@reactor.run
end


@thread_pool.auto_reap! if options[:reaping_time]
@thread_pool.auto_trim! if options[:auto_trim_time]
@thread_pool.auto_trim! if @min_threads != @max_threads && options[:auto_trim_time]

@check, @notify = Puma::Util.pipe unless @notify

Expand Down
35 changes: 35 additions & 0 deletions test/test_puma_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def server_run(**options, &block)
@server = Puma::Server.new block || @app, @events, options
@bind_port = (@server.add_tcp_listener @host, 0).addr[1]
@server.run
ensure
@pool = @server.instance_variable_get(:@thread_pool)
end

def test_http10_req_to_http10_resp
Expand Down Expand Up @@ -2098,4 +2100,37 @@ def test_cl_and_te_smuggle
assert_includes body, "Content-Length: 144\r\n"
assert_equal 1, response.scan("HTTP/1.1 200 OK").size
end

def test_auto_trim_with_variable_pool_size
auto_trim_time = 3
server_run(min_threads: 1, max_threads: 2, auto_trim_time: auto_trim_time)
sleep 1 # wait for possible initial trim on run
thread_pool_expect true, :trim, nil, after: auto_trim_time
end

def test_auto_trim_with_fixed_pool_size
auto_trim_time = 3
server_run(min_threads: 2, max_threads: 2, auto_trim_time: auto_trim_time)
sleep 1 # wait for possible initial trim on run
thread_pool_expect false, :trim, nil, after: auto_trim_time
end

private

def thread_pool_expect(should_expect, expect_method, *expect_args, after: nil)
mock_pool = Minitest::Mock.new
mock_pool.expect(expect_method, *expect_args) if should_expect

@pool.singleton_class.class_eval do
define_method(expect_method) do |*args, **kwargs|
raise "unexpected #{expect_method} called on Puma::Threadpool" unless should_expect

mock_pool.public_send(expect_method, *args, **kwargs)
end
end

sleep after if after

assert mock_pool.verify # assert to satisfy proveit
end
end

0 comments on commit 5f41574

Please sign in to comment.