Hello Aviram!
On Wed, May 8, 2013 at 4:30 AM, Aviram Cohen wrote:
> We've talked before about making some changes in location.capture so that
> in a case of a lot of data returned by the subrequest, there would be some
> kind of an interleaving between the main request and the subrequest, in
> which the buffers of the subrequest would be passed.
>
> I've worked on several versions of this feature, and got to the following
> one (fully working):
> - If you call location.capture with an extra argument set to true, then the
> subrequest would be performed in a "streaming mode".
> - The function location.capture would return with the following parameters:
> - headers - The headers of the subrequest.
> - status - The status of the subrequest.
> - buffer - The first buffer of the subrequest. If the subrequest's
> response body is empty - nil.
> - Afterwards, each call to the function ngx.location.get_subrequest_buffer
> would get the next subrequest buffer. If there are no more buffers, nil is
> returned.
>
Looking good to me :) But I'd rather introduce another API for this
instead of patching ngx.location.capture. How about a separate
ngx.location.capture_stream() function and some OO sugar just as in
the cosocket API? For example,
local subreq, err = ngx.location.capture_stream(...)
local status = subreq:status()
local headers = subreq:headers()
while true do
local first_chunk, err = subreq:read_chunk()
if not first_chunk then
if err then
-- processing the error
end
break -- seen last data chunk
end
-- processing the data chunk
end
Well, just my two cents ;)
> The change I've made currently works only for location.capture and not
> capture_multi, as capture_multi would mean to hold a state for each
> subrequest in the streaming mode, which is kind of clumsy.
>
Well, that's fine :) We can always add another
ngx.location.capture_multi_streams() function in the future ;)
> The change is a bit different than what we've talked about, but after
> struggling with Nginx for a while, this was the best solution I was able to
> write.
> Does this sound like a change that you'd accept?
>
Yes, it does :)
Thank you for your contribution!
BTW, I'm cc'ing the openresty-en mailing list. I think people in the
community may be interested too :)
Best regards,
-agentzh