]> git.kaiwu.me - nginx.git/commitdiff
Core: reusing connections in advance.
authorMaxim Dounin <mdounin@mdounin.ru>
Mon, 10 Aug 2020 15:53:07 +0000 (18:53 +0300)
committerMaxim Dounin <mdounin@mdounin.ru>
Mon, 10 Aug 2020 15:53:07 +0000 (18:53 +0300)
Reworked connections reuse, so closing connections is attempted in
advance, as long as number of free connections is less than 1/16 of
worker connections configured.  This ensures that new connections can
be handled even if closing a reusable connection requires some time,
for example, for a lingering close (ticket #2017).

The 1/16 ratio is selected to be smaller than 1/8 used for disabling
accept when working with accept mutex, so nginx will try to balance
new connections to different workers first, and will start reusing
connections only if this won't help.

src/core/ngx_connection.c

index 91e1b3b2e228c746ca881a86655e9b64e1d9506c..c082d0dac08acce4cd79eb950275d2a23697c753 100644 (file)
@@ -1107,12 +1107,9 @@ ngx_get_connection(ngx_socket_t s, ngx_log_t *log)
         return NULL;
     }
 
-    c = ngx_cycle->free_connections;
+    ngx_drain_connections((ngx_cycle_t *) ngx_cycle);
 
-    if (c == NULL) {
-        ngx_drain_connections((ngx_cycle_t *) ngx_cycle);
-        c = ngx_cycle->free_connections;
-    }
+    c = ngx_cycle->free_connections;
 
     if (c == NULL) {
         ngx_log_error(NGX_LOG_ALERT, log, 0,
@@ -1298,7 +1295,9 @@ ngx_drain_connections(ngx_cycle_t *cycle)
     ngx_queue_t       *q;
     ngx_connection_t  *c;
 
-    if (cycle->reusable_connections_n == 0) {
+    if (cycle->free_connection_n > cycle->connection_n / 16
+        || cycle->reusable_connections_n == 0)
+    {
         return;
     }