Welcome! Log In Create A New Profile

Advanced

Implementing a non-blocking delay between request and response

January 14, 2010 02:26PM
Hi there,

I'm trying to figure out how to implement a non blocking delay between when a request comes into a module, and when it is responded too.

For example, consider a blocking delay:

[code]
static ngx_int_t
ngx_http_my_module_handler(ngx_http_request_t *r)
{
r->headers_out.status = NGX_HTTP_OK
r->header_only = 1;
r->headers_out.content_length_n = 0;

ngx_sleep(10);

rc = ngx_http_send_header(r);
ngx_http_finalize_request(r, rc);
return NGX_DONE;
}
[/code]

Which would allow a client to connect, wait for 10 seconds, and then send the response. That's the behaviour I want, but such a wait as implemented here is blocking, that is, while the first request is waiting to be served, all the other requests are blocked until it completed. I want to make it non blocking so that while one is being processed (and waiting), others can come into the system and be handled (which may themselves wait, or may not).

So after some research, I found out about nginx timers, and it seems I should be able to set a timer/event hander to implement the wait and be non-blocking:

[code]
static void ngx_http_my_module_delay_handler(ngx_event_t *ev);

static ngx_int_t
ngx_http_my_module_handler(ngx_http_request_t *r)
{
r->headers_out.status = NGX_HTTP_OK
r->header_only = 1;
r->headers_out.content_length_n = 0;

ngx_event_t *wakeupFromSleep;
wakeupFromSleep = ngx_pcalloc(r->pool, sizeof(ngx_event_t));

if (wakeupFromSleep == NULL)
ngx_log_stderr(0, "Wakeupfromsleep null");

wakeupFromSleep->handler = ngx_http_my_module_delay_handler;
wakeupFromSleep->data = r;
ngx_log_stderr(0, "Sleeping for 30 seconds");
ngx_add_timer(wakeupFromSleep, 30);

return NGX_DONE;
}

static void
ngx_http_my_module_delay_handler(ngx_event_t *ev)
{
int rc;
ngx_http_request_t *r = ev->data;
ngx_log_stderr(0, "Done waiting. Sending response.");

rc = ngx_http_send_header(r);
ngx_http_finalize_request(r, rc);
ngx_del_timer(ev);
}
[/code]

Unfortunately, when I connect (say with telnet), and send my request, the connection immediately closes instead of waiting. In addition, I never see the "Done waiting. Sending response." message, indicating to me that the event isn't waking up.

My guess as to why the connection is closing immediately has to do with my returning NGX_DONE in ngx_http_my_module_handler, but I can't figure out what else to return that would allow it not to close, and wait for the timer. I can't figure out why the timer event isn't raising, though I suspect it has to do with the connection immediately returning. I think it's just something I'm missing in the setup, but I'm not sure (My view of how timers should work like this could also be flawed).

Any advice you have would be helpful! Thanks.
Subject Author Posted

Implementing a non-blocking delay between request and response

Tronman January 14, 2010 02:26PM

Re: Implementing a non-blocking delay between request and response

Marcus Clyne January 14, 2010 08:02PM

Re: Implementing a non-blocking delay between request and response

agentzh January 14, 2010 08:52PM

Re: Implementing a non-blocking delay between request and response

agentzh January 14, 2010 09:38PM

Re: Implementing a non-blocking delay between request and response

Tronman January 15, 2010 09:43AM

Re: Implementing a non-blocking delay between request and response

Tronman January 15, 2010 10:02AM

Re: Implementing a non-blocking delay between request and response

Tronman January 15, 2010 12:28PM

Re: Implementing a non-blocking delay between request and response

Tronman January 15, 2010 01:25PM

Re: Implementing a non-blocking delay between request and response

Tronman January 15, 2010 02:11PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 267
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready