On 14-07-2014 18:14, Jason Clark wrote:
I'm curious if there's a good way to detect from a Rack middleware
whether a response is streaming or not.
From what I can see, my options are:
1 - In the Controller note we've included `ActionController::Live`
somewhere the middleware can access it
2 - Look at the response object coming back to Rack to see if its
class is `ActionController::Live::Response`
3 - Look at the `stream` (if available) on the response object
returned to Rack to check if it's a `ActionController::Live::Buffer`
All of these ideas seem pretty brittle and liable to break across new
versions of Rails and/or new streaming mechanisms that might come
along. Is there something I'm missing?
Hi Jason,
Actually (2) is not possible using the public API if I remember
correctly because the response is wrapped inside a Rack::BodyProxy (or
something like that). That's why I used (3) when I needed to detect if
streaming was enabled.
Another option would be to use the same convention as Rack::ETag
middleware and do not attempt to read the response if the Cache-Control
header is set to no-cache. ActionController::Live will set this header
on the first response.stream.write call (although there's still a bug
that will happen and affect the ETag middleware as well if the action
calls response.commit! before response.stream.write).
This might be a better and less intrusive work-around until Rack support
some new API to let the middlewares know whether it's a streamed
response or not.
HTH,
Rodrigo.
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.