rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / Atom feed
From: John Firebaugh <john.firebaugh@gmail.com>
To: rubyonrails-core@googlegroups.com, rack-devel@googlegroups.com
Subject: Rack::Response#body vs ActionDispatch::Response#body
Date: Tue, 30 Nov 2010 14:52:17 -0800	[thread overview]
Message-ID: <AANLkTinqR4gAi4AaqtFeJQ=40+2Mv_EddM-Z=FiXJfiz@mail.gmail.com> (raw)

The definitions of #body in Rack::Response and
ActionDispatch::Response are competing and causing performance
problems with streaming responses. Rack::Response provides
attr_accessor :body, while ActionDispatch::Response overrides that as
follows:

    def body
      str = ''
      each { |part| str << part.to_s }
      str
    end

This is problematic in instances where a streaming body is used, due
to the definition of Rack::Response#close:

    def close
      body.close if body.respond_to?(:close)
    end

Given these definitions, executing the Rack protocol on an
ActionDispatch::Response instance (calling '#each', then '#close')
causes the body to be enumerated twice -- once via the expected call
to #each, and a second time via the call chain #close -> #body ->
#each. See https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4554
for a user report of this problem.

This could be fixed by using the instance variable directly:

    def close
      @body.close if @body.respond_to?(:close)
    end

However, I think ActionDispatch::Response's #body override is
dangerous and violates the principle of least surprise by removing the
symmetry between #body and #body=. IMHO, it would be better to remove
the override. If the concatenating behavior is necessary, supply a
method named #body_text or a similar name that makes it clear that it
disables streaming. This option causes numerous Rails test failures,
however, as many tests are written to expect #body to return a string.

Thoughts?

             reply	other threads:[~2010-11-30 23:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-30 22:52 John Firebaugh [this message]
2010-11-30 23:27 ` Rack::Response#body vs ActionDispatch::Response#body Konstantin Haase
2010-12-01  0:52   ` John Firebaugh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://groups.google.com/group/rack-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTinqR4gAi4AaqtFeJQ=40+2Mv_EddM-Z=FiXJfiz@mail.gmail.com' \
    --to=rack-devel@googlegroups.com \
    --cc=rubyonrails-core@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).