Disable Passenger's Response Buffering

technicalpickles edited this page May 14, 2012 · 2 revisions

From Passenger's documentation

When turned on, application-generated responses are buffered in memory. By buffering responses, protection is provided against slow HTTP clients that can not read your response immediately. For example, consider an HTTP client that’s on a dial-up modem link, and your application instance generates a 2 MB response. If response buffering is turned off then your application instance will be blocked until the entire 2 MB has been sent out to the HTTP client. This disallows your application instance to do any useful work in the mean time. By enabling response buffering, Phusion Passenger will read the application response as quickly as possible and will take care of slow clients.

However, keep in mind that enabling this option will make streaming responses impossible. Consider for example this piece of Rails code:

render :text => lambda { |response, output|
    10.times do |i|
        output.write("entry #{i}\n")
        output.flush
        sleep 1
    end
}

…or this piece of Rack code:

class Response
    def each
        10.times do |i|
            yield("entry #{i}\n")
            sleep 1
        end
    end
end

app = lambda do |env|
    [200, { "Content-Type" => "text/plain" }, Response.new]
end

When response buffering is turned on, Phusion Passenger will wait until the application is done sending the entire response before forwarding it to the client. The client will not receive anything for 10 seconds, after which it receives the entire response at once. When response buffering is turned off, it works as expected: the client receives an "entry X" message every second for 10 seconds.

The default value is on.

The PassengerBufferResponse directive should be turned off if responses can be huge. Because entire responses are buffered in memory when turned on.

If your app needs to turn this off, it can be done through config/moonshine.yml:

:passenger:
  :buffer_response: false

Requires at least revision 37eb71fcdad6a318ee398dcb41dfe25db2af94e1.