Hello!
On Sun, Dec 7, 2014 at 7:26 AM, Guido wrote:
>
> Well... It seems that was a backend problem, I've modified the code of the
> backend to honor the keepalive and now it's working
Alas. We've wasted so much time here ;)
> but I'm having another
> problem, nginx response time is equal to the longest response of /prod or
> /dev. I mean, if /prod takes 10ms to answer and /dev 4secs the client will
> receive /prod answers after 4 secs. The same thing the other way also. So,
> the ngx.eof() hack seems to stooped working.
>
You should only see such things if you have enabled the downstream
keep-alive and these slow requests are subsequent requests on the
same downstream (keep-alive) connection (note for the first request on
each downstream connection, the client should receive the response
data without any extra delay).
To quote the official documentation for ngx.eof():
https://github.com/openresty/lua-nginx-module#ngxeof
"When you disable the HTTP 1.1 keep-alive feature for your downstream
connections, you can rely on descent HTTP clients to close the
connection actively for you when you call this method. This trick can
be used do back-ground jobs without letting the HTTP clients to wait
on the connection, as in the following example:
location = /async {
keepalive_timeout 0;
content_by_lua '
ngx.say("got the task!")
ngx.eof() -- a descent HTTP client will close the connection
at this point
-- access MySQL, PostgreSQL, Redis, Memcached, and etc here...
';
}
But if you create subrequests to access other locations configured by
Nginx upstream modules, then you should configure those upstream
modules to ignore client connection abortions if they are not by
default. For example, by default the standard ngx_http_proxy_module
will terminate both the subrequest and the main request as soon as the
client closes the connection, so it is important to turn on the
proxy_ignore_client_abort directive in your location block configured
by ngx_http_proxy_module:
proxy_ignore_client_abort on;
A better way to do background jobs is to use the ngx.timer.at API."
> So, my question is, It's possible to "detach" /dev's answers with keep alive
> connections enabled?
>
As mentioned by the quoted documentation above, to fully "detach" from
the original request, you have to use the ngx.timer.at API to create a
timer. But for obvious reasons, you cannot use subrequests in a timer
handler (because you have already detached from the original request,
how is it possible to further create subrequests of it?). You can use
one of the 3rd-party lua-resty-http* libraries out there, like
lua-resty-http-simple [1] instead.
The ngx.eof() work-around still works if you fulfill the requirements
stated in the documentation quoted above. If not, please provide a
minimal and standalone example that can reproduce the problem.
BTW, please do not quote more than 50 lines of the original mail text
in a single bulk. It is quote annoying for the readers and is a waste
of bandwidth.
Regards,
-agentzh
[1] https://github.com/bakins/lua-resty-http-simple