Skip to content

Commit

Permalink
[close #1577] Negative Backpressure Metric
Browse files Browse the repository at this point in the history
This PR introduces the `pool_capacity` stat which can be used as a “negative back pressure metric”. 

What does a “negative backpressure metric” mean? It means when this number is low, we have a higher amount of backpressure. When it is zero it means that our worker has no ability to process additional requests. This information could be used to scale out by adding additional servers. When that happens the requests should be re-distributed between the extra server and the “pool capacity” number for each individual server should go up.
  • Loading branch information
schneems committed May 4, 2018
1 parent 119b6eb commit 8b10df8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/puma/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def worker(index, master)
begin
b = server.backlog || 0
r = server.running || 0
payload = %Q!#{base_payload}{ "backlog":#{b}, "running":#{r} }\n!
t = server.pool_capacity || 0
payload = %Q!#{base_payload}{ "backlog":#{b}, "running":#{r}, "pool_capacity": #{t} }\n!
io << payload
rescue IOError
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
Expand Down
5 changes: 5 additions & 0 deletions lib/puma/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def running
@thread_pool and @thread_pool.spawned
end


def pool_capacity
@thread_pool and @thread_pool.waiting
end

# Lopez Mode == raw tcp apps

def run_lopez_mode(background=true)
Expand Down
3 changes: 2 additions & 1 deletion lib/puma/single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Single < Runner
def stats
b = @server.backlog || 0
r = @server.running || 0
%Q!{ "backlog": #{b}, "running": #{r} }!
t = @server.pool_capacity || 0
%Q!{ "backlog": #{b}, "running": #{r}, "pool_capacity": #{t} }!
end

def restart
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def initialize(min, max, *extra, &block)
@clean_thread_locals = false
end

attr_reader :spawned, :trim_requested
attr_reader :spawned, :trim_requested, :waiting
attr_accessor :clean_thread_locals

def self.clean_thread_locals
Expand Down

0 comments on commit 8b10df8

Please sign in to comment.