handle NGX_AGAIN properly

Yichun Zhang (agentzh) agentzh at gmail.com
Mon Jul 15 04:57:33 UTC 2013


Hello!

On Sun, Jul 14, 2013 at 8:43 PM, Julien Zefi wrote:
>
> Sorry by bother you again but i still cannot figure out how some internals
> are not working as i expect. I have take in count your suggestions and wrote
> a new test case (file attached).
>

1. You should simply call  ngx_http_output_filter(r, NULL); in your
r->write_event_handler, but you set r->write_event_handler to
ngx_http_test_stream_handler which always emits brand new data. I'm
guessing you don't really understand how the ngx_http_writer and
ngx_http_set_write_handler functions are implemented in the Nginx
core. Look harder.

2. You should not set r->header_only = 1 in your case because you're
actually sending out the response body. Ensure that you know how a
flag works before you start using it.

3. Another obvious mistake is that you incorrectly perform

    r->main->count++;

without decrementing it by calling ngx_http_finalize_request, which
will certainly lead to request hang. Ensure that you understand this
flag before using it.

> The test case writes 12.3KB of data every 1ms, at some point it will raise
> NGX_AGAIN but from there is not recovering, it keeps in the same state
> forever, do you see any specific problem when handling the exception ?
>

This is trivial to implement by writing some Lua code using ngx_lua module:

    location /t {
        content_by_lua '
           local message = "..."
           for i = 1, 100 do
               ngx.print(message)
               ngx.flush(true)
               ngx.sleep(0.001)
           end
        ';
    }

Maybe you can just use ngx_lua for your purposes without all the pain
of understanding the nginx internals (you seem to lack a lot of
knowledge here). If you insist in writing your own nginx C module,
then just check out how ngx_lua implements all the APIs demonstrated
in the example above. You can also check out the official
documentation of ngx_lua:

  http://wiki.nginx.org/HttpLuaModule

Best regards,
-agentzh



More information about the nginx-devel mailing list