<div dir="ltr"># HG changeset patch<br># User Jussi Maki <jusmaki at <a href="http://gmail.com">gmail.com</a>><br># Date 1617816597 -10800<br>#      Wed Apr 07 20:29:57 2021 +0300<br># Node ID 3699288ff20a3e51ee4b7689898ce0241f64f0f5<br># Parent  e2e9e0fae74734b28974c64daacc492d751b4781<br>Upstream: new "keepalive_max_connection_duration" directive<br><br>Added a new keepalive_max_connection duration which provides<br>the time in milliseconds for the upstream block on how long<br>the connection should be kept connected. The current keepalive<br>directives either define the idle time or the number of requests<br>but there is no elapsed time-based parameter. <br><br>The elapsed time-based connection parameter is useful in a case<br>when there are multiple backends and the connection should be <br>evenly load balanced to them and the response times for upstream<br>requests vary.<br><br>diff -r e2e9e0fae747 -r 3699288ff20a src/core/ngx_connection.h<br>--- a/src/core/ngx_connection.h Mon Apr 05 20:14:16 2021 +0300<br>+++ b/src/core/ngx_connection.h Wed Apr 07 20:29:57 2021 +0300<br>@@ -191,6 +191,8 @@<br> #if (NGX_THREADS || NGX_COMPAT)<br>     ngx_thread_task_t  *sendfile_task;<br> #endif<br>+<br>+    ngx_msec_t          connection_started_time;<br> };<br> <br> <br>diff -r e2e9e0fae747 -r 3699288ff20a src/http/modules/ngx_http_upstream_keepalive_module.c<br>--- a/src/http/modules/ngx_http_upstream_keepalive_module.c     Mon Apr 05 20:14:16 2021 +0300<br>+++ b/src/http/modules/ngx_http_upstream_keepalive_module.c     Wed Apr 07 20:29:57 2021 +0300<br>@@ -14,6 +14,7 @@<br>     ngx_uint_t                         max_cached;<br>     ngx_uint_t                         requests;<br>     ngx_msec_t                         timeout;<br>+    ngx_msec_t                         duration;<br> <br>     ngx_queue_t                        cache;<br>     ngx_queue_t                        free;<br>@@ -100,6 +101,13 @@<br>       offsetof(ngx_http_upstream_keepalive_srv_conf_t, requests),<br>       NULL },<br> <br>+    { ngx_string("keepalive_max_connection_duration"),<br>+      NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,<br>+      ngx_conf_set_num_slot,<br>+      NGX_HTTP_SRV_CONF_OFFSET,<br>+      offsetof(ngx_http_upstream_keepalive_srv_conf_t, duration),<br>+      NULL },<br>+<br>       ngx_null_command<br> };<br><div>@@ -387,7 +395,13 @@<br>     item->socklen = pc->socklen;<br>     ngx_memcpy(&item->sockaddr, pc->sockaddr, pc->socklen);<br> <br>-    if (c->read->ready) {<br>+    if (kp->conf->duration != NGX_CONF_UNSET_MSEC &&<br>+       ngx_current_msec - c->connection_started_time > kp->conf->duration) {<br>+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,<br>+                      "free keepalive peer: expired connection %p", c);<br>+        c->close = 1;<br>+    }<br>+    if (c->close || c->read->ready) {<br>         ngx_http_upstream_keepalive_close_handler(c->read);<br>     }<br> <br>@@ -515,6 +529,7 @@<br> <br>     conf->timeout = NGX_CONF_UNSET_MSEC;<br>     conf->requests = NGX_CONF_UNSET_UINT;<br>+    conf->duration = NGX_CONF_UNSET_MSEC;<br> <br>     return conf;<br> }<br>diff -r e2e9e0fae747 -r 3699288ff20a src/http/ngx_http_upstream.c<br>--- a/src/http/ngx_http_upstream.c      Mon Apr 05 20:14:16 2021 +0300<br>+++ b/src/http/ngx_http_upstream.c      Wed Apr 07 20:29:57 2021 +0300<br>@@ -2017,6 +2017,10 @@<br>         u->state->connect_time = ngx_current_msec - u->start_time;<br>     }<br> <br>+    if (!c->connection_started_time) {<br>+       c->connection_started_time = ngx_current_msec;<br>+    }<br>+<br>     if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {<br>         ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);<br>         return;<br></div><div><br></div></div>