Keepalived Connections Reset after reloading the configuration (HUP Signal)

tored nginx-forum at forum.nginx.org
Wed Oct 7 08:29:50 UTC 2020


Hi,

I'm looking into the same issue; how to improve graceful shutdown when using
keep-alive connections.

It seems nginx at some point had support for doing graceful shutdown (if i
read the code correctly):
http://hg.nginx.org/nginx/rev/03f1133f24e8

But it was removed at a later stage:
http://hg.nginx.org/nginx/rev/5e6142609e48

The feature was removed due to negative impact on CPU usage, maybe the
process could be handled in ngx_http_finalize_connection(ngx_http_request_t
*r) ? That may be a better option as it should only trigger when
ngx_terminate and ngx_quit flags are active:

diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 2a0528c6..0b8e05fa 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -2735,6 +2735,15 @@ ngx_http_finalize_connection(ngx_http_request_t *r)
         return;
     }
 
+    if (ngx_terminate
+         || ngx_quit
+         && r->keepalive)
+    {
+        r->keepalive = 0;
+        ngx_http_close_request(r, 0);
+        return;
+    }
+
     if (clcf->lingering_close == NGX_HTTP_LINGERING_ALWAYS
         || (clcf->lingering_close == NGX_HTTP_LINGERING_ON
             && (r->lingering_close


Does this seem something that could work ?


One other option could be to expand the function
ngx_close_idle_connections(ngx_cycle_t *cycle) to check if the connection is
using keepalive, if so, set keeplive to 0. I  believe that would ensure that
during graceful shutdown, an keepalive connection would be able to perform
one request, then asked to close the connection:

diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c
index c082d0da..df90c4f1 100644
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -1334,8 +1334,10 @@ ngx_close_idle_connections(ngx_cycle_t *cycle)
 {
     ngx_uint_t         i;
     ngx_connection_t  *c;
+    ngx_http_request_t *r;
 
     c = cycle->connections;
+    r = cycle->
 
     for (i = 0; i < cycle->connection_n; i++) {
 
@@ -1345,6 +1347,11 @@ ngx_close_idle_connections(ngx_cycle_t *cycle)
             c[i].close = 1;
             c[i].read->handler(c[i].read);
         }
+
+        if (r->keepalive) {
+            r->keepalive = 0;
+        }
+
     }
 }

I understand that the second example is not a fully working example.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,197927,289673#msg-289673



More information about the nginx mailing list