From 0ad75089971217ab77c6a63142c27269b272e020 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Mon, 7 Nov 2022 19:00:27 -0800 Subject: [PATCH] Improve performance by avoiding method cache busts (#19957) Switch to monkey-patching http.rb rather than a runtime extend of each response, so as to avoid busting the global method cache. A guard is included that will provide developer feedback in development and test environments should the monkey patch ever collide. --- app/lib/request.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/lib/request.rb b/app/lib/request.rb index 648aa30850d5fd..1ea86862dbb709 100644 --- a/app/lib/request.rb +++ b/app/lib/request.rb @@ -62,8 +62,6 @@ def perform end begin - response = response.extend(ClientLimit) - # If we are using a persistent connection, we have to # read every response to be able to move forward at all. # However, simply calling #to_s or #flush may not be safe, @@ -181,6 +179,14 @@ def body_with_limit(limit = 1.megabyte) end end + if ::HTTP::Response.methods.include?(:body_with_limit) && !Rails.env.production? + abort 'HTTP::Response#body_with_limit is already defined, the monkey patch will not be applied' + else + class ::HTTP::Response + include Request::ClientLimit + end + end + class Socket < TCPSocket class << self def open(host, *args)