Forum: Ruby on Rails Rails 3.2 and Streaming using response_body

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C2a9b1cbe4fe9db10a34fc672cff2cf6?d=identicon&s=25 Matthew Fordham (Guest)
on 2012-03-29 22:09
(Received via mailing list)
I am working on a streaming download (CSV) from Rails 3.2 and am coming
against an issue of the initial page request taking a long time. The
following controller code illustrates my issue:

      self.response_body = do |y|
        10_000_000.times do
          y << "Hello World"

With the above, the response does seem like its streaming (from a server
than can support it... Unicorn, in my case). That said, before it starts
streaming it hangs for a much longer time than I'd like. If I change it
the following, it starts much faster:

      self.response_body = do |y|
        1000.times do
          y << "Hello World"

My understanding is that the response should begin with the first
of the loop, but it seems the larger loops are causing that initial load
time to lengthen. If each iteration is output as it happens, shouldn't
take the same amount of time to kick off the streaming process,
of how many total iterations there will be???
Here is an explanation of the technique I am attempting. Maybe I am
misinterpreting or missing a step?:

Thanks for any insight you may have!
Cb146d04eaf373d3946d75fd1c4ed17e?d=identicon&s=25 Cam Allen (Guest)
on 2013-08-15 21:19
(Received via mailing list)
Hi Matthew,

I have built a CSV streaming response that suffers from the same problem
that you've described. It seems the larger content amount will delay the
response headers being sent, thus signifying the start of the stream,
save dialog in browser. It seems this has to do with they web server
used and possibly buffering responses before sending the stream but
found a directive to fix it.

I've tested with webrick, puma, thin and passenger+nginx and dug into
configurations to no avail. Note: only passenger and puma stream
and they both do so with the delayed response for large streams.

Did you every find out what was causing the delay on larger streams?
Cb146d04eaf373d3946d75fd1c4ed17e?d=identicon&s=25 Cam Allen (Guest)
on 2013-08-22 17:12
(Received via mailing list)
For future reference, it turns out that Rack is calling each twice on
response_body, see!topic/rack-devel...
Once i added some checks to only stream once on the second call (rack
closing the connection) it worked as expected! Note: if you only respond
the first call then the response will be empty.

Hopefully this saves someone some time in the future!
This topic is locked and can not be replied to.