mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Add automatic pool trimming
This commit is contained in:
parent
c5aad50eb4
commit
f11cd81470
3 changed files with 63 additions and 0 deletions
|
@ -25,6 +25,7 @@ module Puma
|
|||
attr_accessor :min_threads
|
||||
attr_accessor :max_threads
|
||||
attr_accessor :persistent_timeout
|
||||
attr_accessor :auto_trim_time
|
||||
|
||||
# Create a server for the rack app +app+.
|
||||
#
|
||||
|
@ -45,6 +46,7 @@ module Puma
|
|||
|
||||
@min_threads = 0
|
||||
@max_threads = 16
|
||||
@auto_trim_time = 1
|
||||
|
||||
@thread = nil
|
||||
@thread_pool = nil
|
||||
|
@ -118,6 +120,10 @@ module Puma
|
|||
process_client(client)
|
||||
end
|
||||
|
||||
if @auto_trim_time
|
||||
@thread_pool.auto_trim!(@auto_trim_time)
|
||||
end
|
||||
|
||||
@thread = Thread.new do
|
||||
begin
|
||||
check = @check
|
||||
|
|
|
@ -24,6 +24,8 @@ module Puma
|
|||
|
||||
@workers = []
|
||||
|
||||
@auto_trim = nil
|
||||
|
||||
min.times { spawn_thread }
|
||||
end
|
||||
|
||||
|
@ -97,9 +99,40 @@ module Puma
|
|||
end
|
||||
end
|
||||
|
||||
class AutoTrim
|
||||
def initialize(pool, timeout)
|
||||
@pool = pool
|
||||
@timeout = timeout
|
||||
@running = false
|
||||
end
|
||||
|
||||
def start!
|
||||
@running = true
|
||||
|
||||
@thread = Thread.new do
|
||||
while @running
|
||||
@pool.trim
|
||||
sleep @timeout
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
@running = false
|
||||
@thread.wakeup
|
||||
end
|
||||
end
|
||||
|
||||
def auto_trim!(timeout=5)
|
||||
@auto_trim = AutoTrim.new(self, timeout)
|
||||
@auto_trim.start!
|
||||
end
|
||||
|
||||
# Tell all threads in the pool to exit and wait for them to finish.
|
||||
#
|
||||
def shutdown
|
||||
@auto_trim.stop if @auto_trim
|
||||
|
||||
@spawned.times do
|
||||
@todo << Stop
|
||||
end
|
||||
|
|
|
@ -99,4 +99,28 @@ class TestThreadPool < Test::Unit::TestCase
|
|||
|
||||
assert_equal 1, pool.spawned
|
||||
end
|
||||
|
||||
def test_autotrim
|
||||
finish = false
|
||||
pool = new_pool(1, 2) { Thread.pass until finish }
|
||||
|
||||
pool << 1
|
||||
pool << 2
|
||||
|
||||
assert_equal 2, pool.spawned
|
||||
|
||||
finish = true
|
||||
|
||||
pause
|
||||
|
||||
assert_equal 2, pool.spawned
|
||||
|
||||
pool.auto_trim! 1
|
||||
|
||||
sleep 1
|
||||
|
||||
pause
|
||||
|
||||
assert_equal 1, pool.spawned
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue