Simple timeout module

Maxim Dounin mdounin at mdounin.ru
Wed May 20 18:36:29 UTC 2015


Hello!

On Wed, May 20, 2015 at 01:03:19PM -0400, donatasm wrote:

> I'm trying to build a simple timeout module using nginx timers. At the
> beginning of a request I'm firing up a timer and after time interval elapses
> I want to check if request has already completed, and if not, finalize it,
> for example it with NGX_HTTP_REQUEST_TIME_OUT. I have created a filter
> module. I'm creating a timer in filter headers:
> 
> static ngx_int_t simple_timeout_filter_headers(ngx_http_request_t* request)
> {
>   ngx_event_t* timeout_event;
> 
>   timeout_event = ngx_pcalloc(request->pool, sizeof(ngx_event_t));
>   if (timeout_event == NULL)
>   {
>     return NGX_ERROR;
>   }
> 
>   timeout_event->handler = simple_timeout_handler;
>   timeout_event->data = request;
>   timeout_event->log = request->connection->log;
> 
>   ngx_log_debug0(NGX_LOG_DEBUG_HTTP, request->connection->log, 0,
>       "SIMPLE TIMEOUT TIMER START");
> 
>   ngx_add_timer(timeout_event, 3000); /* wait for 3 seconds */
> 
>   return next_header_filter(request);
> }
> 
> Simple timeout handler looks like this:
> 
> static void simple_timeout_handler(ngx_event_t* timeout_event)
> {
>   ngx_log_debug0(NGX_LOG_DEBUG_HTTP, timeout_event->log, 0,
>       "SIMPLE TIMEOUT TIMER END");
> }
> 
> And it works if I issue a request, and wait for a timer to fire. If I issue
> several requests while the previous timer is already in progress, I get a
> SEGFAULT. 

At least you don't remove the timer if the request completes 
before the timer is triggered.  This is enough to trigger a 
segmentation fault.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list