diff --git a/src/event/modules/ngx_devpoll_module.c b/src/event/modules/ngx_devpoll_module.c index ad44743a7..13971ad4d 100644 --- a/src/event/modules/ngx_devpoll_module.c +++ b/src/event/modules/ngx_devpoll_module.c @@ -29,7 +29,7 @@ static int nevents; static ngx_event_t **change_index; -static ngx_event_t timer_queue; +static ngx_event_t *timer_queue; /* */ @@ -55,13 +55,15 @@ int ngx_devpoll_init(int max_connections, ngx_log_t *log) ngx_alloc(sizeof(ngx_event_t *) * DEVPOLL_NCHANGES, log), NGX_ERROR); - timer_queue.timer_prev = &timer_queue; - timer_queue.timer_next = &timer_queue; + timer_queue = ngx_event_init_timer(log); + if (timer_queue == NULL) { + return NGX_ERROR; + } #if !(USE_DEVPOLL) ngx_event_actions.add = ngx_devpoll_add_event; ngx_event_actions.del = ngx_devpoll_del_event; - ngx_event_actions.timer = ngx_devpoll_add_timer; + ngx_event_actions.timer = ngx_event_add_timer; ngx_event_actions.process = ngx_devpoll_process_events; #endif @@ -116,6 +118,8 @@ int ngx_devpoll_del_event(ngx_event_t *ev, int event, u_int flags) return NGX_OK; } + /* we need to restore second event if it exists */ + if (event == NGX_READ_EVENT) { e = c->write; event = POLLOUT; @@ -186,15 +190,16 @@ int ngx_devpoll_set_event(ngx_event_t *ev, int event, u_int flags) int ngx_devpoll_process_events(ngx_log_t *log) { int events, n, i; - u_int timer, delta; + ngx_msec_t timer, delta; ngx_err_t err; ngx_event_t *ev; ngx_connection_t *c; struct dvpoll dvp; struct timeval tv; - if (timer_queue.timer_next != &timer_queue) { - timer = timer_queue.timer_next->timer_delta; + timer = ngx_event_find_timer(); + + if (timer) { gettimeofday(&tv, NULL); delta = tv.tv_sec * 1000 + tv.tv_usec / 1000; @@ -300,59 +305,9 @@ int ngx_devpoll_process_events(ngx_log_t *log) } } - if (timer != INFTIM && timer_queue.timer_next != &timer_queue) { - if (delta >= timer_queue.timer_next->timer_delta) { - for ( ;; ) { - ev = timer_queue.timer_next; - - if (ev == &timer_queue || delta < ev->timer_delta) { - break; - } - - delta -= ev->timer_delta; - - ngx_del_timer(ev); - ev->timedout = 1; - if (ev->event_handler(ev) == NGX_ERROR) { - ev->close_handler(ev); - } - } - - } else { - timer_queue.timer_next->timer_delta -= delta; - } + if (timer != INFTIM) { + ngx_event_expire_timers(delta); } return NGX_OK; } - - -void ngx_devpoll_add_timer(ngx_event_t *ev, ngx_msec_t timer) -{ - ngx_event_t *e; - -#if (NGX_DEBUG_EVENT) - ngx_connection_t *c = (ngx_connection_t *) ev->data; - ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer); -#endif - - if (ev->timer_next || ev->timer_prev) { - ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set"); - return; - } - - for (e = timer_queue.timer_next; - e != &timer_queue && timer > e->timer_delta; - e = e->timer_next) - { - timer -= e->timer_delta; - } - - ev->timer_delta = timer; - - ev->timer_next = e; - ev->timer_prev = e->timer_prev; - - e->timer_prev->timer_next = ev; - e->timer_prev = ev; -} diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c index 792798ef8..abf3b6e61 100644 --- a/src/event/modules/ngx_kqueue_module.c +++ b/src/event/modules/ngx_kqueue_module.c @@ -28,7 +28,7 @@ static struct kevent *change_list, *event_list; static unsigned int nchanges; static int nevents; -static ngx_event_t timer_queue; +static ngx_event_t *timer_queue; /* */ @@ -51,20 +51,19 @@ int ngx_kqueue_init(int max_connections, ngx_log_t *log) ngx_test_null(change_list, ngx_alloc(change_size, log), NGX_ERROR); ngx_test_null(event_list, ngx_alloc(event_size, log), NGX_ERROR); - if (ngx_event_init_timer(log) == NGX_ERROR) { + timer_queue = ngx_event_init_timer(log); + if (timer_queue == NULL) { return NGX_ERROR; } -#if 0 - timer_queue.timer_prev = &timer_queue; - timer_queue.timer_next = &timer_queue; -#endif - #if !(USE_KQUEUE) ngx_event_actions.add = ngx_kqueue_add_event; ngx_event_actions.del = ngx_kqueue_del_event; - ngx_event_actions.timer = ngx_kqueue_add_timer; + ngx_event_actions.timer = ngx_event_add_timer; ngx_event_actions.process = ngx_kqueue_process_events; + + ngx_event_flags = NGX_HAVE_LEVEL_EVENT + |NGX_HAVE_ONESHOT_EVENT|NGX_HAVE_CLEAR_EVENT; #endif return NGX_OK; @@ -170,7 +169,7 @@ int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags) int ngx_kqueue_process_events(ngx_log_t *log) { int events, i; - u_int timer, delta; + ngx_msec_t timer, delta; ngx_event_t *ev; struct timeval tv; struct timespec ts, *tp; @@ -190,22 +189,6 @@ int ngx_kqueue_process_events(ngx_log_t *log) tp = NULL; } -#if 0 - if (timer_queue.timer_next != &timer_queue) { - timer = timer_queue.timer_next->timer_delta; - ts.tv_sec = timer / 1000; - ts.tv_nsec = (timer % 1000) * 1000000; - tp = &ts; - gettimeofday(&tv, NULL); - delta = tv.tv_sec * 1000 + tv.tv_usec / 1000; - - } else { - timer = 0; - delta = 0; - tp = NULL; - } -#endif - #if (NGX_DEBUG_EVENT) ngx_log_debug(log, "kevent timer: %d" _ timer); #endif @@ -288,61 +271,5 @@ int ngx_kqueue_process_events(ngx_log_t *log) ngx_event_expire_timers(delta); } -#if 0 - if (timer && timer_queue.timer_next != &timer_queue) { - if (delta >= timer_queue.timer_next->timer_delta) { - for ( ;; ) { - ev = timer_queue.timer_next; - - if (ev == &timer_queue || delta < ev->timer_delta) { - break; - } - - delta -= ev->timer_delta; - - ngx_del_timer(ev); - ev->timedout = 1; - if (ev->event_handler(ev) == NGX_ERROR) { - ev->close_handler(ev); - } - } - - } else { - timer_queue.timer_next->timer_delta -= delta; - } - } -#endif - return NGX_OK; } - - -void ngx_kqueue_add_timer(ngx_event_t *ev, ngx_msec_t timer) -{ - ngx_event_t *e; - -#if (NGX_DEBUG_EVENT) - ngx_connection_t *c = (ngx_connection_t *) ev->data; - ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer); -#endif - - if (ev->timer_next || ev->timer_prev) { - ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set"); - return; - } - - for (e = timer_queue.timer_next; - e != &timer_queue && timer > e->timer_delta; - e = e->timer_next) - { - timer -= e->timer_delta; - } - - ev->timer_delta = timer; - - ev->timer_next = e; - ev->timer_prev = e->timer_prev; - - e->timer_prev->timer_next = ev; - e->timer_prev = ev; -} diff --git a/src/event/modules/ngx_poll_module.c b/src/event/modules/ngx_poll_module.c index 4b2434a5d..e27031cfc 100644 --- a/src/event/modules/ngx_poll_module.c +++ b/src/event/modules/ngx_poll_module.c @@ -17,7 +17,7 @@ static unsigned int nevents; static ngx_event_t **event_index; static ngx_event_t **ready_index; -static ngx_event_t timer_queue; +static ngx_event_t *timer_queue; /* */ int ngx_poll_init(int max_connections, ngx_log_t *log) @@ -36,12 +36,14 @@ int ngx_poll_init(int max_connections, ngx_log_t *log) nevents = 0; - timer_queue.timer_prev = &timer_queue; - timer_queue.timer_next = &timer_queue; + timer_queue = ngx_event_init_timer(log); + if (timer_queue == NULL) { + return NGX_ERROR; + } ngx_event_actions.add = ngx_poll_add_event; ngx_event_actions.del = ngx_poll_del_event; - ngx_event_actions.timer = ngx_poll_add_timer; + ngx_event_actions.timer = ngx_event_add_timer; ngx_event_actions.process = ngx_poll_process_events; return NGX_OK; @@ -139,13 +141,14 @@ int ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags) int ngx_poll_process_events(ngx_log_t *log) { int i, ready, nready, found; - u_int timer, delta; + ngx_msec_t timer, delta; ngx_err_t err; ngx_event_t *ev; ngx_connection_t *c; - if (timer_queue.timer_next != &timer_queue) { - timer = timer_queue.timer_next->timer_delta; + timer = ngx_event_find_timer(); + + if (timer) { delta = ngx_msec(); } else { @@ -253,58 +256,9 @@ int ngx_poll_process_events(ngx_log_t *log) ngx_log_error(NGX_LOG_ALERT, log, 0, "poll ready != events"); } - if (timer != INFTIM && timer_queue.timer_next != &timer_queue) { - if (delta >= timer_queue.timer_next->timer_delta) { - for ( ;; ) { - ev = timer_queue.timer_next; - - if (ev == &timer_queue || delta < ev->timer_delta) { - break; - } - - delta -= ev->timer_delta; - - ngx_del_timer(ev); - ev->timedout = 1; - if (ev->event_handler(ev) == NGX_ERROR) { - ev->close_handler(ev); - } - } - - } else { - timer_queue.timer_next->timer_delta -= delta; - } + if (timer != INFTIM) { + ngx_event_expire_timers(delta); } return NGX_OK; } - -void ngx_poll_add_timer(ngx_event_t *ev, ngx_msec_t timer) -{ - ngx_event_t *e; - -#if (NGX_DEBUG_EVENT) - ngx_connection_t *c = (ngx_connection_t *) ev->data; - ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer); -#endif - - if (ev->timer_next || ev->timer_prev) { - ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set"); - return; - } - - for (e = timer_queue.timer_next; - e != &timer_queue && timer > e->timer_delta; - e = e->timer_next) - { - timer -= e->timer_delta; - } - - ev->timer_delta = timer; - - ev->timer_next = e; - ev->timer_prev = e->timer_prev; - - e->timer_prev->timer_next = ev; - e->timer_prev = ev; -} diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c index 5b62dfd04..fe461c3f5 100644 --- a/src/event/modules/ngx_select_module.c +++ b/src/event/modules/ngx_select_module.c @@ -23,11 +23,11 @@ static int max_write; static int max_fd; #endif -static u_int nevents; +static int nevents; static ngx_event_t **event_index; static ngx_event_t **ready_index; -static ngx_event_t timer_queue; +static ngx_event_t *timer_queue; /* */ int ngx_select_init(int max_connections, ngx_log_t *log) @@ -57,12 +57,14 @@ int ngx_select_init(int max_connections, ngx_log_t *log) nevents = 0; - timer_queue.timer_prev = &timer_queue; - timer_queue.timer_next = &timer_queue; + timer_queue = ngx_event_init_timer(log); + if (timer_queue == NULL) { + return NGX_ERROR; + } ngx_event_actions.add = ngx_select_add_event; ngx_event_actions.del = ngx_select_del_event; - ngx_event_actions.timer = ngx_select_add_timer; + ngx_event_actions.timer = ngx_event_add_timer; ngx_event_actions.process = ngx_select_process_events; #if (WIN32) @@ -176,7 +178,8 @@ int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags) int ngx_select_process_events(ngx_log_t *log) { int ready, found, nready; - u_int i, timer, delta; + u_int i; + ngx_msec_t timer, delta; ngx_event_t *ev; ngx_connection_t *c; struct timeval tv, *tp; @@ -184,12 +187,12 @@ int ngx_select_process_events(ngx_log_t *log) work_read_fd_set = master_read_fd_set; work_write_fd_set = master_write_fd_set; - if (timer_queue.timer_next != &timer_queue) { - timer = timer_queue.timer_next->timer_delta; + timer = ngx_event_find_timer(); + + if (timer) { tv.tv_sec = timer / 1000; tv.tv_usec = (timer % 1000) * 1000; tp = &tv; - delta = ngx_msec(); } else { @@ -309,58 +312,9 @@ int ngx_select_process_events(ngx_log_t *log) ngx_log_error(NGX_LOG_ALERT, log, 0, "select ready != events"); } - if (timer && timer_queue.timer_next != &timer_queue) { - if (delta >= timer_queue.timer_next->timer_delta) { - for ( ;; ) { - ev = timer_queue.timer_next; - - if (ev == &timer_queue || delta < ev->timer_delta) { - break; - } - - delta -= ev->timer_delta; - - ngx_del_timer(ev); - ev->timedout = 1; - if (ev->event_handler(ev) == NGX_ERROR) { - ev->close_handler(ev); - } - } - - } else { - timer_queue.timer_next->timer_delta -= delta; - } + if (timer) { + ngx_event_expire_timers(delta); } return NGX_OK; } - -void ngx_select_add_timer(ngx_event_t *ev, ngx_msec_t timer) -{ - ngx_event_t *e; - -#if (NGX_DEBUG_EVENT) - ngx_connection_t *c = (ngx_connection_t *) ev->data; - ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer); -#endif - - if (ev->timer_next || ev->timer_prev) { - ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set"); - return; - } - - for (e = timer_queue.timer_next; - e != &timer_queue && timer > e->timer_delta; - e = e->timer_next) - { - timer -= e->timer_delta; - } - - ev->timer_delta = timer; - - ev->timer_next = e; - ev->timer_prev = e->timer_prev; - - e->timer_prev->timer_next = ev; - e->timer_prev = ev; -} diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c index 9a5b73005..a6addd568 100644 --- a/src/event/ngx_event.c +++ b/src/event/ngx_event.c @@ -32,7 +32,7 @@ ngx_event_t *ngx_read_events, *ngx_write_events; #if 0 ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT; -#elif 0 +#elif 1 ngx_event_type_e ngx_event_type = NGX_POLL_EVENT; #else ngx_event_type_e ngx_event_type = NGX_KQUEUE_EVENT; @@ -54,6 +54,8 @@ ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT; #endif +int ngx_event_flags; + ngx_event_actions_t ngx_event_actions; /* ngx_event_type_e order */ diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h index 1b0ac062d..76e31672b 100644 --- a/src/event/ngx_event.h +++ b/src/event/ngx_event.h @@ -56,6 +56,7 @@ struct ngx_event_s { unsigned ready:1; unsigned timedout:1; unsigned blocked:1; + unsigned timer_set:1; unsigned process:1; unsigned read_discarded:1; @@ -69,6 +70,17 @@ struct ngx_event_s { unsigned eof:1; int error; #endif + +#if 0 + void *thr_ctx; /* event thread context if $(CC) doesn't + understand __thread declaration + and pthread_getspecific() is too costly */ + +#if (NGX_EVENT_T_PADDING) + int padding[NGX_EVENT_T_PADDING]; /* event should not cross + cache line in SMP */ +#endif +#endif }; typedef enum { @@ -96,20 +108,24 @@ typedef struct { } ngx_event_actions_t; -/* -NGX_LEVEL_EVENT (default) select, poll, /dev/poll, kqueue - requires to read whole data -NGX_ONESHOT_EVENT select, poll, /dev/poll(*), kqueue, epoll(*) - (*) - additional syscall -NGX_CLEAR_EVENT kqueue, epoll -NGX_AIO_EVENT overlapped, aio_read, aioread - no need to add or delete events +/* Event filter requires to read/write the whole data - + select, poll, /dev/poll, kqueue. */ +#define NGX_HAVE_LEVEL_EVENT 1 +/* Event filter deleted after notification - select, poll, kqueue. + /dev/poll, epoll implemetned with additional syscall */ +#define NGX_HAVE_ONESHOT_EVENT 2 +/* Event filter notify only changes - kqueue, epoll */ +#define NGX_HAVE_CLEAR_EVENT 4 +/* No nedd to add or delete event filters - overlapped, aio_read, aioread */ +#define NGX_HAVE_AIO_EVENT 8 -NGX_CLOSE_EVENT kqueue: kqueue deletes events for file that closed - /dev/poll: need to flush events before closing -*/ +/* Event filter is deleted before closing file. Has no meaning for select, poll. + kqueue: kqueue deletes event filters for file that closed + so we need only to delete filters in user-level batch array + /dev/poll: we need to flush POLLREMOVE event before closing file + epoll: ??? */ +#define NGX_CLOSE_EVENT 1 -#define NGX_CLOSE_EVENT 1 #if (HAVE_KQUEUE) @@ -209,6 +225,7 @@ extern ngx_connection_t *ngx_connections; #if !(USE_KQUEUE) extern ngx_event_actions_t ngx_event_actions; extern ngx_event_type_e ngx_event_type; +extern int ngx_event_flags; #endif diff --git a/src/event/ngx_event_timer.c b/src/event/ngx_event_timer.c index 8b2616160..e20839d9e 100644 --- a/src/event/ngx_event_timer.c +++ b/src/event/ngx_event_timer.c @@ -10,30 +10,32 @@ #include /* STUB */ -#define NGX_TIMER_HASH_SIZE 5 +#define NGX_TIMER_QUEUE_NUM 5 -ngx_event_t *ngx_timer_queue; -int ngx_timer_hash_size; -static int ngx_timer_cur_queue; +/* should be per-thread */ +static ngx_event_t *ngx_timer_queue; +static int ngx_timer_cur_queue; +/* */ +static int ngx_timer_queue_num; -int ngx_event_init_timer(ngx_log_t *log) +ngx_event_t *ngx_event_init_timer(ngx_log_t *log) { int i; - ngx_timer_hash_size = NGX_TIMER_HASH_SIZE; + ngx_timer_queue_num = NGX_TIMER_QUEUE_NUM; ngx_timer_cur_queue = 0; ngx_test_null(ngx_timer_queue, - ngx_alloc(ngx_timer_hash_size * sizeof(ngx_event_t), log), - NGX_ERROR); + ngx_alloc(ngx_timer_queue_num * sizeof(ngx_event_t), log), + NULL); - for (i = 0; i < ngx_timer_hash_size; i++) { + for (i = 0; i < ngx_timer_queue_num; i++) { ngx_timer_queue[i].timer_prev = &ngx_timer_queue[i]; ngx_timer_queue[i].timer_next = &ngx_timer_queue[i]; } - return NGX_OK; + return ngx_timer_queue; } @@ -63,7 +65,7 @@ void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer) } ngx_timer_cur_queue++; - if (ngx_timer_cur_queue >= ngx_timer_hash_size) { + if (ngx_timer_cur_queue >= ngx_timer_queue_num) { ngx_timer_cur_queue = 0; } @@ -77,13 +79,38 @@ void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer) } +int ngx_event_find_timer(void) +{ + int i; + ngx_msec_t timer; + + timer = NGX_MAX_MSEC; + + for (i = 0; i < ngx_timer_queue_num; i++) { + if (ngx_timer_queue[i].timer_next == &ngx_timer_queue[i]) { + continue; + } + + if (timer > ngx_timer_queue[i].timer_next->timer_delta) { + timer = ngx_timer_queue[i].timer_next->timer_delta; + } + } + + if (timer == NGX_MAX_MSEC) { + return 0; + } else { + return timer; + } +} + + void ngx_event_expire_timers(ngx_msec_t timer) { int i; ngx_msec_t delta; ngx_event_t *ev; - for (i = 0; i < ngx_timer_hash_size; i++) { + for (i = 0; i < ngx_timer_queue_num; i++) { delta = timer; diff --git a/src/event/ngx_event_timer.h b/src/event/ngx_event_timer.h index 24cb46c0a..7f1816aa6 100644 --- a/src/event/ngx_event_timer.h +++ b/src/event/ngx_event_timer.h @@ -9,36 +9,13 @@ #include -int ngx_event_init_timer(ngx_log_t *log); +ngx_event_t *ngx_event_init_timer(ngx_log_t *log); void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer); +int ngx_event_find_timer(void); +void ngx_event_expire_timers(ngx_msec_t timer); + extern ngx_event_t *ngx_timer_queue; -extern int ngx_timer_hash_size; - - -ngx_inline static int ngx_event_find_timer() -{ - int i; - ngx_msec_t timer; - - timer = NGX_MAX_MSEC; - - for (i = 0; i < ngx_timer_hash_size; i++) { - if (ngx_timer_queue[i].timer_next == &ngx_timer_queue[i]) { - continue; - } - - if (timer > ngx_timer_queue[i].timer_next->timer_delta) { - timer = ngx_timer_queue[i].timer_next->timer_delta; - } - } - - if (timer == NGX_MAX_MSEC) { - return 0; - } else { - return timer; - } -} ngx_inline static void ngx_event_del_timer(ngx_event_t *ev) diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h index 8d0596ab4..bdef89995 100644 --- a/src/http/ngx_http.h +++ b/src/http/ngx_http.h @@ -148,7 +148,7 @@ struct ngx_http_request_s { unsigned lingering_close:1; unsigned header_read:1; - unsigned header_timeout:1; + unsigned header_timeout_set:1; unsigned logging:1; diff --git a/src/http/ngx_http_event.c b/src/http/ngx_http_event.c index 679210558..5856ebff6 100644 --- a/src/http/ngx_http_event.c +++ b/src/http/ngx_http_event.c @@ -115,34 +115,40 @@ int ngx_http_init_connection(ngx_connection_t *c) #if (HAVE_DEFERRED_ACCEPT) if (ev->ready) { return ngx_http_init_request(ev); - } else { -#endif - ngx_add_timer(ev, c->post_accept_timeout); -#if (USE_KQUEUE) - return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT); -#else -#if (HAVE_AIO_EVENT) - if (ngx_event_type == NGX_AIO_EVENT) - return ngx_http_init_request(ev); - else -#endif -#if (HAVE_CLEAR_EVENT) - if (ngx_event_type == NGX_KQUEUE_EVENT) - return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT); - else -#endif - return ngx_add_event(ev, NGX_READ_EVENT, NGX_LEVEL_EVENT); -#endif /* USE_KQUEUE */ -#if (HAVE_DEFERRED_ACCEPT) } #endif + + ngx_add_timer(ev, c->post_accept_timeout); + ev->timer_set = 1; + +#if (USE_KQUEUE) + + return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT); + +#else + +#if (HAVE_CLEAR_EVENT) + if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) { + return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT); + } +#endif + +#if (HAVE_AIO_EVENT) + if (ngx_event_flags & NGX_HAVE_AIO_EVENT) { + return ngx_http_init_request(ev); + } +#endif + + return ngx_add_event(ev, NGX_READ_EVENT, NGX_LEVEL_EVENT); + +#endif /* USE_KQUEUE */ } static int ngx_http_init_request(ngx_event_t *ev) { - ngx_connection_t *c; - ngx_http_request_t *r; + ngx_connection_t *c; + ngx_http_request_t *r; c = (ngx_connection_t *) ev->data; c->sent = 0; @@ -179,7 +185,6 @@ static int ngx_http_init_request(ngx_event_t *ev) ev->event_handler = ngx_http_process_request_header; r->state_handler = ngx_http_process_request_line; - r->header_timeout = 1; return ngx_http_process_request_header(ev); } @@ -187,9 +192,9 @@ static int ngx_http_init_request(ngx_event_t *ev) static int ngx_http_process_request_header(ngx_event_t *ev) { - int n, rc; - ngx_connection_t *c ; - ngx_http_request_t *r; + int n, rc; + ngx_connection_t *c; + ngx_http_request_t *r; ngx_http_log_ctx_t *ctx; c = (ngx_connection_t *) ev->data; @@ -207,10 +212,16 @@ static int ngx_http_process_request_header(ngx_event_t *ev) r->header_in->end - r->header_in->last.mem); if (n == NGX_AGAIN) { - if (r->header_timeout) { - r->header_timeout = 0; - ngx_del_timer(ev); + if (!r->header_timeout_set) { + + if (ev->timer_set) { + ngx_del_timer(ev); + } else { + ev->timer_set = 1; + } + ngx_add_timer(ev, ngx_http_client_header_timeout); + r->header_timeout_set = 1; } return NGX_AGAIN; } @@ -244,20 +255,25 @@ static int ngx_http_process_request_header(ngx_event_t *ev) if (rc == NGX_OK) { /* HTTP header done */ - if (r->header_timeout) { - r->header_timeout = 0; + if (ev->timer_set) { ngx_del_timer(ev); - ngx_add_timer(ev, ngx_http_client_header_timeout); + ev->timer_set = 0; } return ngx_http_event_request_handler(r); - } else { + } else { /* NGX_AGAIN */ + + if (!r->header_timeout_set) { + + if (ev->timer_set) { + ngx_del_timer(ev); + } else { + ev->timer_set = 1; + } - if (r->header_timeout) { - r->header_timeout = 0; - ngx_del_timer(ev); ngx_add_timer(ev, ngx_http_client_header_timeout); + r->header_timeout_set = 1; } return rc; @@ -430,14 +446,20 @@ static int ngx_http_process_request_header_line(ngx_http_request_t *r) static int ngx_http_event_request_handler(ngx_http_request_t *r) { - int rc; - ngx_msec_t timeout; + int rc; + ngx_msec_t timeout; + ngx_event_t *rev, *wev; - ngx_del_timer(r->connection->read); - r->header_timeout = 0; + rev = r->connection->read; + wev = r->connection->write; + + if (rev->timer_set) { + ngx_del_timer(rev); + rev->timer_set = 0; + } r->state_handler = NULL; - r->connection->read->event_handler = ngx_http_block_read; + rev->event_handler = ngx_http_block_read; rc = ngx_http_handler(r); @@ -448,10 +470,10 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) /* handler has done its work but transfer is not completed */ if (rc == NGX_AGAIN) { #if (HAVE_CLEAR_EVENT) - if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, + if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) == NGX_ERROR) { #else - if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, + if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_ONESHOT_EVENT) == NGX_ERROR) { #endif return ngx_http_close_request(r); @@ -462,13 +484,13 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) r->connection->sent); timeout = (ngx_msec_t) (r->connection->sent * 10); ngx_log_debug(r->connection->log, "timeout: %d" _ timeout); - ngx_add_timer(r->connection->write, timeout); + ngx_add_timer(wev, timeout); } else { - ngx_add_timer(r->connection->write, 10000); + ngx_add_timer(wev, 10000); } - r->connection->write->event_handler = ngx_http_writer; + wev->event_handler = ngx_http_writer; return rc; } @@ -496,7 +518,7 @@ static int ngx_http_event_request_handler(ngx_http_request_t *r) ngx_http_close_request(r); r->connection->buffer->pos.mem = r->connection->buffer->last.mem = r->connection->buffer->start; - r->connection->read->event_handler = ngx_http_keepalive_handler; + rev->event_handler = ngx_http_keepalive_handler; } @@ -527,12 +549,17 @@ static int ngx_http_writer(ngx_event_t *ev) ngx_log_debug(ev->log, "sent: " QD_FMT _ c->sent); ngx_log_debug(ev->log, "timeout: %d" _ timeout); - ngx_del_timer(ev); + if (ev->timer_set) { + ngx_del_timer(ev); + } else { + ev->timer_set = 1; + } + ngx_add_timer(ev, timeout); } if (ev->oneshot) - if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, + if (ngx_add_event(ev, NGX_WRITE_EVENT, NGX_ONESHOT_EVENT) == NGX_ERROR) { return ngx_http_close_request(r); } @@ -575,12 +602,19 @@ static int ngx_http_block_read(ngx_event_t *ev) int ngx_http_discard_body(ngx_http_request_t *r) { + ngx_event_t *ev; + + ev = r->connection->read; + ngx_log_debug(r->connection->log, "set discard body"); - ngx_del_timer(r->connection->read); + if (ev->timer_set) { + ngx_del_timer(ev); + ev->timer_set = 0; + } if (r->client_content_length) - r->connection->read->event_handler = ngx_http_read_discarded_body; + ev->event_handler = ngx_http_read_discarded_body; return NGX_OK; } @@ -665,19 +699,27 @@ static int ngx_http_keepalive_handler(ngx_event_t *ev) static int ngx_http_set_lingering_close(ngx_http_request_t *r) { + ngx_event_t *ev; ngx_http_core_loc_conf_t *lcf; + ev = r->connection->read; + lcf = (ngx_http_core_loc_conf_t *) ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); r->lingering_time = ngx_time() + lcf->lingering_time; r->connection->read->event_handler = ngx_http_lingering_close_handler; - ngx_del_timer(r->connection->read); - ngx_add_timer(r->connection->read, lcf->lingering_timeout); + if (ev->timer_set) { + ngx_del_timer(ev); + } else { + ev->timer_set = 1; + } - if (r->connection->read->blocked) { - if (ngx_add_event(r->connection->read, NGX_READ_EVENT, + ngx_add_timer(ev, lcf->lingering_timeout); + + if (ev->blocked) { + if (ngx_add_event(ev, NGX_READ_EVENT, #if (HAVE_CLEAR_EVENT) NGX_CLEAR_EVENT) == NGX_ERROR) #else @@ -688,8 +730,7 @@ static int ngx_http_set_lingering_close(ngx_http_request_t *r) } } - if (ngx_shutdown_socket(r->connection->fd, NGX_WRITE_SHUTDOWN) == -1) - { + if (ngx_shutdown_socket(r->connection->fd, NGX_WRITE_SHUTDOWN) == -1) { ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_socket_errno, ngx_shutdown_socket_n " failed"); return ngx_http_close_request(r); @@ -749,7 +790,11 @@ static int ngx_http_lingering_close_handler(ngx_event_t *ev) timer = lcf->lingering_timeout; } - ngx_del_timer(ev); + if (ev->timer_set) { + ngx_del_timer(ev); + } else { + ev->timer_set = 1; + } ngx_add_timer(ev, timer); return NGX_OK;