Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't buffer things that shouldn't be buffered. #51508

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ioquatix
Copy link
Contributor

@ioquatix ioquatix commented Apr 6, 2024

Assigning ActionDispatch::Response to self.response causes the response body to be buffered (and potentially evaluated multiple times).

class StreamingController < ApplicationController
  def simple
    body = Enumerator.new do |enumerator|
      enumerator << "." * 1024
      
      100.times do |i|
        enumerator << "This is line #{i}\n"
        sleep 0.1
      end
    end

    # Works, puma, falcon, Rails 7.1
    # self.response = Rack::Response[200, {"content-type" => "text/plain"}, body]

    # Doesn't work because the response is buffered:
    self.response = ActionDispatch::Response.new(200, {"content-type" => "text/plain"}, body)
  end
end

In both Puma and Falcon, the response is buffered without this change.

cc @willcosgrove

@rails-bot rails-bot bot added the actionpack label Apr 6, 2024
@ioquatix ioquatix force-pushed the actiondispatch-response-body-to_ary branch from 46caa06 to 39e1787 Compare April 6, 2024 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant