Skip to content

Commit

Permalink
Close keepalive connections after MAX_FAST_INLINE requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nateberkopec committed May 11, 2021
1 parent 6dfb8bc commit df72887
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/puma/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,20 @@ def process_client(client, buffer)

requests += 1

check_for_more_data = @status == :run
# Closing keepalive sockets after they've made a reasonable
# number of requests allows Puma to service many connections
# fairly, even when the number of concurrent connections exceeds
# the size of the threadpool. It also allows cluster mode Pumas
# to keep load evenly distributed across workers, because clients
# are randomly assigned a new worker when opening a new connection.
#
# Previously, Puma would kick connections in this conditional back
# to the reactor. However, because this causes the todo set to increase
# in size, the wait_until_full mutex would never unlock, leaving
# any additional connections unserviced.
break if requests >= @max_fast_inline

if requests >= @max_fast_inline
# This will mean that reset will only try to use the data it already
# has buffered and won't try to read more data. What this means is that
# every client, independent of their request speed, gets treated like a slow
# one once every max_fast_inline requests.
check_for_more_data = false
end
check_for_more_data = @status == :run

next_request_ready = with_force_shutdown(client) do
client.reset(check_for_more_data)
Expand Down

0 comments on commit df72887

Please sign in to comment.