PATCH Upstream: new "keepalive_max_connection_duration" directive

Jussi Maki jusmaki at gmail.com
Wed Apr 7 17:51:38 UTC 2021


# HG changeset patch
# User Jussi Maki <jusmaki at gmail.com>
# Date 1617816597 -10800
#      Wed Apr 07 20:29:57 2021 +0300
# Node ID 3699288ff20a3e51ee4b7689898ce0241f64f0f5
# Parent  e2e9e0fae74734b28974c64daacc492d751b4781
Upstream: new "keepalive_max_connection_duration" directive

Added a new keepalive_max_connection duration which provides
the time in milliseconds for the upstream block on how long
the connection should be kept connected. The current keepalive
directives either define the idle time or the number of requests
but there is no elapsed time-based parameter.

The elapsed time-based connection parameter is useful in a case
when there are multiple backends and the connection should be
evenly load balanced to them and the response times for upstream
requests vary.

diff -r e2e9e0fae747 -r 3699288ff20a src/core/ngx_connection.h
--- a/src/core/ngx_connection.h Mon Apr 05 20:14:16 2021 +0300
+++ b/src/core/ngx_connection.h Wed Apr 07 20:29:57 2021 +0300
@@ -191,6 +191,8 @@
 #if (NGX_THREADS || NGX_COMPAT)
     ngx_thread_task_t  *sendfile_task;
 #endif
+
+    ngx_msec_t          connection_started_time;
 };


diff -r e2e9e0fae747 -r 3699288ff20a
src/http/modules/ngx_http_upstream_keepalive_module.c
--- a/src/http/modules/ngx_http_upstream_keepalive_module.c     Mon Apr 05
20:14:16 2021 +0300
+++ b/src/http/modules/ngx_http_upstream_keepalive_module.c     Wed Apr 07
20:29:57 2021 +0300
@@ -14,6 +14,7 @@
     ngx_uint_t                         max_cached;
     ngx_uint_t                         requests;
     ngx_msec_t                         timeout;
+    ngx_msec_t                         duration;

     ngx_queue_t                        cache;
     ngx_queue_t                        free;
@@ -100,6 +101,13 @@
       offsetof(ngx_http_upstream_keepalive_srv_conf_t, requests),
       NULL },

+    { ngx_string("keepalive_max_connection_duration"),
+      NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_num_slot,
+      NGX_HTTP_SRV_CONF_OFFSET,
+      offsetof(ngx_http_upstream_keepalive_srv_conf_t, duration),
+      NULL },
+
       ngx_null_command
 };
@@ -387,7 +395,13 @@
     item->socklen = pc->socklen;
     ngx_memcpy(&item->sockaddr, pc->sockaddr, pc->socklen);

-    if (c->read->ready) {
+    if (kp->conf->duration != NGX_CONF_UNSET_MSEC &&
+       ngx_current_msec - c->connection_started_time > kp->conf->duration)
{
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+                      "free keepalive peer: expired connection %p", c);
+        c->close = 1;
+    }
+    if (c->close || c->read->ready) {
         ngx_http_upstream_keepalive_close_handler(c->read);
     }

@@ -515,6 +529,7 @@

     conf->timeout = NGX_CONF_UNSET_MSEC;
     conf->requests = NGX_CONF_UNSET_UINT;
+    conf->duration = NGX_CONF_UNSET_MSEC;

     return conf;
 }
diff -r e2e9e0fae747 -r 3699288ff20a src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c      Mon Apr 05 20:14:16 2021 +0300
+++ b/src/http/ngx_http_upstream.c      Wed Apr 07 20:29:57 2021 +0300
@@ -2017,6 +2017,10 @@
         u->state->connect_time = ngx_current_msec - u->start_time;
     }

+    if (!c->connection_started_time) {
+       c->connection_started_time = ngx_current_msec;
+    }
+
     if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
         ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
         return;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20210407/8e9c615f/attachment.htm>


More information about the nginx-devel mailing list