Skip to content

Commit

Permalink
Improve performance by avoiding method cache busts (mastodon#19957)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
raggi authored Nov 8, 2022
1 parent 9f4930e commit 833d9c2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/lib/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 833d9c2

Please sign in to comment.