changeset 1135:03f1133f24e8

close keep-alive connections in the shuting down processes
author Igor Sysoev <igor@sysoev.ru>
date Mon, 19 Mar 2007 13:20:15 +0000
parents 384c92c210f9
children 68f30ab68bb7
files src/core/ngx_connection.h src/http/ngx_http_request.c src/os/unix/ngx_process_cycle.c
diffstat 3 files changed, 30 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/ngx_connection.h
+++ b/src/core/ngx_connection.h
@@ -143,6 +143,9 @@ struct ngx_connection_s {
     unsigned            error:1;
     unsigned            destroyed:1;
 
+    unsigned            idle:1;
+    unsigned            close:1;
+
     unsigned            sendfile:1;
     unsigned            sndlowat:1;
     unsigned            tcp_nodelay:2;   /* ngx_connection_tcp_nodelay_e */
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -2032,6 +2032,8 @@ ngx_http_set_keepalive(ngx_http_request_
     r->http_state = NGX_HTTP_KEEPALIVE_STATE;
 #endif
 
+    c->idle = 1;
+
     if (rev->ready) {
         ngx_http_keepalive_handler(rev);
     }
@@ -2050,7 +2052,7 @@ ngx_http_keepalive_handler(ngx_event_t *
 
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
 
-    if (rev->timedout) {
+    if (rev->timedout || c->close) {
         ngx_http_close_connection(c);
         return;
     }
@@ -2139,6 +2141,8 @@ ngx_http_keepalive_handler(ngx_event_t *
     c->log->handler = ngx_http_log_error;
     c->log->action = "reading client request line";
 
+    c->idle = 0;
+
     ngx_http_init_request(rev);
 }
 
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -664,6 +664,8 @@ ngx_master_process_exit(ngx_cycle_t *cyc
 static void
 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
 {
+    ngx_uint_t         i;
+    ngx_connection_t  *c;
 #if (NGX_THREADS)
     ngx_int_t          n;
     ngx_err_t          err;
@@ -717,12 +719,27 @@ ngx_worker_process_cycle(ngx_cycle_t *cy
 #endif
 
     for ( ;; ) {
-        if (ngx_exiting
-            && ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
-        {
-            ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
+
+        if (ngx_exiting) {
+
+            c = cycle->connections;
+
+            for (i = 0; i < cycle->connection_n; i++) {
+
+                /* THREAD: lock */
 
-            ngx_worker_process_exit(cycle);
+                if (c[i].fd != -1 && c[i].idle) {
+                    c[i].close = 1;
+                    c[i].read->handler(c[i].read);
+                }
+            }
+
+            if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
+            {
+                ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
+
+                ngx_worker_process_exit(cycle);
+            }
         }
 
         ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");