Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2 streaming

I am working on a streaming download (CSV) from Rails 3.2 and am coming up against an issue of the initial page request taking a long time. The following controller code illustrates my issue:

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

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 to the following, it starts much faster:

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

My understanding is that the response should begin with the first iteration 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 it take the same amount of time to kick off the streaming process, regardless of how many total iterations there will be???

Thanks for any insight you may have!

EDIT:

Here is an explanation of the technique I am attempting. Maybe I am misinterpreting or missing a step?: http://facebook.stackoverflow.com/questions/3507594/ruby-on-rails-3-streaming-data-through-rails-to-client/4320399#4320399

EDIT:

I think Rack-Cache might be causing my problem... can I turn it off for an individual request?

EDIT and SOLVED:

I was wrong about Rack-Cache. i just needed to add self.response.headers['Last-Modified'] = Time.now.ctime.to_s to my response.

like image 871
Matt Fordham Avatar asked Mar 28 '12 21:03

Matt Fordham


1 Answers

The edited question turned out to contain exactly the answer I needed. Posting it here as an answer.

The answer to getting the Rack handler to stream properly is apparently to add a Last-Modified header to the response:

self.response.headers['Last-Modified'] = Time.now.ctime.to_s
like image 188
mbklein Avatar answered Oct 21 '22 00:10

mbklein