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

Add /gc URL to status server to do a major GC. Add a /gc-status URL for GC.stats. #1384

Merged
merged 1 commit into from Aug 3, 2017
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
8 changes: 8 additions & 0 deletions lib/puma/app/status.rb
Expand Up @@ -55,6 +55,14 @@ def call(env)
return rack_response(200, OK_STATUS)
end

when /\/gc$/
GC.start
return rack_response(200, OK_STATUS)

when /\/gc-stats$/
json = "{" + GC.stat.map { |k, v| "\"#{k}\": #{v}" }.join(",") + "}"
return rack_response(200, json)

when /\/stats$/
return rack_response(200, @cli.stats)
else
Expand Down
4 changes: 2 additions & 2 deletions lib/puma/control_cli.rb
Expand Up @@ -9,7 +9,7 @@
module Puma
class ControlCLI

COMMANDS = %w{halt restart phased-restart start stats status stop reload-worker-directory}
COMMANDS = %w{halt restart phased-restart start stats status stop reload-worker-directory gc gc-stats}

def initialize(argv, stdout=STDOUT, stderr=STDERR)
@state = nil
Expand Down Expand Up @@ -169,7 +169,7 @@ def send_request
end

message "Command #{@command} sent success"
message response.last if @command == "stats"
message response.last if @command == "stats" || @command == "gc-stats"
end

@server.close
Expand Down
54 changes: 54 additions & 0 deletions test/test_cli.rb
Expand Up @@ -145,6 +145,60 @@ def test_control_stop
t.join
end

def test_control_gc_stats
url = "unix://#{@tmp_path}"

cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control", url,
"--control-token", "",
"test/rackup/lobster.ru"], @events

t = Thread.new { cli.run }
t.abort_on_exception = true

wait_booted

s = UNIXSocket.new @tmp_path
s << "GET /gc-stats HTTP/1.0\r\n\r\n"
body = s.read
s.close

lines = body.split("\r\n")
json_line = lines.detect { |l| l[0] == "{" }
pairs = json_line.scan(/\"[^\"]+\": [^,]+/)
gc_stats = {}
pairs.each do |p|
p =~ /\"([^\"]+)\": ([^,]+)/ || raise("Can't parse #{p.inspect}!")
gc_stats[$1] = $2
end
gc_count_before = gc_stats["count"].to_i

s = UNIXSocket.new @tmp_path
s << "GET /gc HTTP/1.0\r\n\r\n"
body = s.read # Ignored
s.close

s = UNIXSocket.new @tmp_path
s << "GET /gc-stats HTTP/1.0\r\n\r\n"
body = s.read
s.close
lines = body.split("\r\n")
json_line = lines.detect { |l| l[0] == "{" }
pairs = json_line.scan(/\"[^\"]+\": [^,]+/)
gc_stats = {}
pairs.each do |p|
p =~ /\"([^\"]+)\": ([^,]+)/ || raise("Can't parse #{p.inspect}!")
gc_stats[$1] = $2
end
gc_count_after = gc_stats["count"].to_i

# Hitting the /gc route should increment the count by 1
assert_equal gc_count_before + 1, gc_count_after

cli.launcher.stop
t.join
end

def test_tmp_control
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", "auto"]
Expand Down