Skip to content

Instantly share code, notes, and snippets.

@nateberkopec
Created May 11, 2021 14:17
Show Gist options
  • Save nateberkopec/4b3ea5676c0d70cbb37c82d54be25837 to your computer and use it in GitHub Desktop.
Save nateberkopec/4b3ea5676c0d70cbb37c82d54be25837 to your computer and use it in GitHub Desktop.
Puma keepalive patch
From df72887170c7ef3614c941c9bdefb4a1f3546ebf Mon Sep 17 00:00:00 2001
From: Nate Berkopec <[email protected]>
Date: Tue, 11 May 2021 07:43:32 -0600
Subject: [PATCH] Close keepalive connections after MAX_FAST_INLINE requests
---
lib/puma/server.rb | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/lib/puma/server.rb b/lib/puma/server.rb
index 3912699c..32bdbd2e 100644
--- a/lib/puma/server.rb
+++ b/lib/puma/server.rb
@@ -447,15 +447,20 @@ module Puma
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)
--
2.30.1 (Apple Git-130)
@MarcelRaschke
Copy link

patch

@curtcurt87
Copy link

patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment