Hello!
On Mon, Jul 28, 2014 at 8:59 AM, kamal pushpad wrote:
> <<<<< First way >>>>>>>
>
> static void ngx_http_test_wev_handler(ngx_http_request_t *r)
[...]
> b->last_buf = 1;
>
Unconditionally setting of last_buf here is wrong. You should test if
the current request is a subrequest because in that case you should
set b->last_in_chain instead.
> r->headers_out.status = NGX_HTTP_OK;
> r->headers_out.content_length_n = sizeof(ngx_hello_world);
> ngx_http_send_header(r);
> ngx_http_output_filter(r, &out);
> ngx_http_finalize_request(r, rc);
I don't see how you define and initialize this "rc" variable. Actually
the value of "rc" here is the key for the correctness of this timer
handler. It *must* be NGX_OK in this context and for your purpose. Any
other values are just wrong and can lead to request hang.
> static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
[...]
> r->write_event_handler = ngx_http_test_wev_handler;
> wev = r->connection->write;
> ngx_add_timer(wev, 2000);
Abusing the write event timer here is logically wrong. This event
timer should only be used for protecting against writing timeouts.
Better create your own dedicated ngx_event_t for your own
special-purpose timer.
> If i directly access /auth_req then it get proper response. but if access
> any other URL which goes through my Lua script then it never give response
> and close connection.
>
This is usually a sign of out-of-sync r->main->count reference count
in your module code. It should also manifest itself when being used in
a main request. You may not notice the issue with main requests when
using well-behaved HTTP clients like "curl" or modern web browsers
because these clients will actively close the connection even when the
server side never does. And that's why I always use
Test::Nginx::Socket to drive all the test suites of my nginx C
modules:
http://search.cpan.org/perldoc?Test%3A%3ANginx%3A%3ASocket
You'd better use it too to save your time (and my time).
Also, enabling the nginx debug logging helps tracking the
r->main->count reference in the whole lifetime of your request:
http://nginx.org/en/docs/debugging_log.html
Just watch the messages with the little thing "c:xxx" (where xxx is a
number, which is r->main->count). They are usually generated by
ngx_http_finalize_request.
> But below code works perfectly fine if i am serving the header and response
> in main handler function instead of creating timer event.
>
> <<<<< second way >>>>>>>
>
This simplest "way" is much less likely to get wrong :) (Though it
still could and may already contain issues.)
> So i am not sure what i am missing in first way ????? do i have do
> something different in first way as it works fine in second way.
>
NGINX C programming is hard, especially for beginners. That's why I
created ngx_lua in the first place and why I recommended you use
ngx_lua exclusively for your requirements. Otherwise you'll have to
spend a lot of time on your nginx C modules (and so will I if I want
to support you along the way :))
Regards,
-agentzh