Skip to content

Commit

Permalink
Add /gc URL to status server to do a major GC. Add a /gc-status URL t…
Browse files Browse the repository at this point in the history
…o get GC.stats as JSON. (#1384)
  • Loading branch information
noahgibbs authored and nateberkopec committed Aug 3, 2017
1 parent b25dc5d commit 0d7a8bb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
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

0 comments on commit 0d7a8bb

Please sign in to comment.