[PATCH] Fix drain logic for small number of connections

Joel Cunningham joel.cunningham at me.com
Wed Dec 7 21:50:32 UTC 2016


I run NGINX in an embedded environment with limited resources.  We have NGINX configured with a small number of maximum connections (worker_connections is 16).  I’ve found the ngx_drain_connection() logic uses a hard-coded value to limit the number of reaped reusable connections.  This works fine with the default 512, but is too aggressive for small number of connections and ends up reaping all reusable connections.  These include new connections just accepted on the previous event loop cycle (which had not yet had a chance to receive a request) at the head of the list.

The below patch switches to the minimum of 32 or 1/4 of the number of connections.  This should be less aggressive, even when the number of connections is slightly larger than 32, e.g. 48 or 64.


# HG changeset patch
# User Joel Cunningham <joel.cunningham at me.com>
# Date 1481145862 21600
#      Wed Dec 07 15:24:22 2016 -0600
# Node ID b103dddcee7322522651f9aca764d499d5822ac1
# Parent  75dbab4ea930bc73cca98d183c2f556eb5125462
Fix drain logic for small number of connections

This commit fixes the ngx_drain_connections logic when maximum number of
connections are small (16/32/64).  Using a hardcoded value of 32 when
the number of connections is small is overlly aggressive and will result
in repeaing the entire (or large portion of) the reusable_connections_queue,
which includes at the tail newly accepted connections that have not received
a request yet

The logic is updated to use the minimum of 1/4 the number of connections
or 32, which ever is smaller

diff -r 75dbab4ea930 -r b103dddcee73 src/core/ngx_connection.c
--- a/src/core/ngx_connection.c	Mon Nov 21 16:03:42 2016 +0300
+++ b/src/core/ngx_connection.c	Wed Dec 07 15:24:22 2016 -0600
@@ -1232,7 +1232,7 @@
     ngx_queue_t       *q;
     ngx_connection_t  *c;
-    for (i = 0; i < 32; i++) {
+    for (i = 0; i < ngx_min(32, ngx_cycle->connection_n / 4); i++) {
         if (ngx_queue_empty(&ngx_cycle->reusable_connections_queue)) {

More information about the nginx-devel mailing list