handle NGX_AGAIN properly

Yichun Zhang (agentzh) agentzh at gmail.com
Wed Jul 10 01:02:35 UTC 2013


Hello!

On Tue, Jul 9, 2013 at 5:12 PM, Julien Zefi wrote:
> But if in some triggered callback by the timer the
> ngx_http_output_filter(..) returns NGX_AGAIN *i assume* NginX will send that
> chain as soon as the socket becomes available again.

This assumption is not correct. Nginx will only flush out the pending
data for you when r->write_event_handler is set to ngx_http_writer.
This only (automatically) happens in ngx_http_finalize_request (by
calling the ngx_http_set_write_handler function to do the assignment
to r->write_event_handler).

> But after that happens,
> how can i restore my timer cycle ?
>

My suggestion is to register your own r->write_event_handler handler
to propagate the pending outputs by calling ngx_http_output_filter
with a NULL chain link pointer yourself. And in that handler, you can
also restore your timer cycle and etc when all the pending outputs
have been flushed out (into the system socket send buffers).

I've been doing something like this in our ngx_lua module. You can
check out the ngx.flush() API function's implementation in particular:

    http://wiki.nginx.org/HttpLuaModule#ngx.flush

Best regards,
-agentzh



More information about the nginx-devel mailing list