changeset 7315:e7b2b907c0f8

Events: moved sockets cloning to ngx_event_init_conf(). Previously, listenings sockets were not cloned if the worker_processes directive was specified after "listen ... reuseport". This also simplifies upcoming configuration check on the number of worker connections, as it needs to know the number of listening sockets before cloning.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 12 Jul 2018 19:50:02 +0300
parents 3dfc1584ad75
children 8f152ca81f5f
files src/core/ngx_connection.c src/core/ngx_connection.h src/event/ngx_event.c src/http/ngx_http.c src/stream/ngx_stream.c
diffstat 5 files changed, 30 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/ngx_connection.c	Wed Jul 11 17:56:51 2018 +0300
+++ b/src/core/ngx_connection.c	Thu Jul 12 19:50:02 2018 +0300
@@ -96,7 +96,7 @@
 
 
 ngx_int_t
-ngx_clone_listening(ngx_conf_t *cf, ngx_listening_t *ls)
+ngx_clone_listening(ngx_cycle_t *cycle, ngx_listening_t *ls)
 {
 #if (NGX_HAVE_REUSEPORT)
 
@@ -104,20 +104,19 @@
     ngx_core_conf_t  *ccf;
     ngx_listening_t   ols;
 
-    if (!ls->reuseport) {
+    if (!ls->reuseport || ls->worker != 0) {
         return NGX_OK;
     }
 
     ols = *ls;
 
-    ccf = (ngx_core_conf_t *) ngx_get_conf(cf->cycle->conf_ctx,
-                                           ngx_core_module);
+    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
     for (n = 1; n < ccf->worker_processes; n++) {
 
         /* create a socket for each worker process */
 
-        ls = ngx_array_push(&cf->cycle->listening);
+        ls = ngx_array_push(&cycle->listening);
         if (ls == NULL) {
             return NGX_ERROR;
         }
--- a/src/core/ngx_connection.h	Wed Jul 11 17:56:51 2018 +0300
+++ b/src/core/ngx_connection.h	Thu Jul 12 19:50:02 2018 +0300
@@ -210,7 +210,7 @@
 
 ngx_listening_t *ngx_create_listening(ngx_conf_t *cf, struct sockaddr *sockaddr,
     socklen_t socklen);
-ngx_int_t ngx_clone_listening(ngx_conf_t *cf, ngx_listening_t *ls);
+ngx_int_t ngx_clone_listening(ngx_cycle_t *cycle, ngx_listening_t *ls);
 ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle);
 ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle);
 void ngx_configure_listening_sockets(ngx_cycle_t *cycle);
--- a/src/event/ngx_event.c	Wed Jul 11 17:56:51 2018 +0300
+++ b/src/event/ngx_event.c	Thu Jul 12 19:50:02 2018 +0300
@@ -410,12 +410,37 @@
 static char *
 ngx_event_init_conf(ngx_cycle_t *cycle, void *conf)
 {
+#if (NGX_HAVE_REUSEPORT)
+    ngx_uint_t        i;
+    ngx_listening_t  *ls;
+#endif
+
     if (ngx_get_conf(cycle->conf_ctx, ngx_events_module) == NULL) {
         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
                       "no \"events\" section in configuration");
         return NGX_CONF_ERROR;
     }
 
+#if (NGX_HAVE_REUSEPORT)
+
+    ls = cycle->listening.elts;
+    for (i = 0; i < cycle->listening.nelts; i++) {
+
+        if (!ls[i].reuseport || ls[i].worker != 0) {
+            continue;
+        }
+
+        if (ngx_clone_listening(cycle, &ls[i]) != NGX_OK) {
+            return NGX_CONF_ERROR;
+        }
+
+        /* cloning may change cycle->listening.elts */
+
+        ls = cycle->listening.elts;
+    }
+
+#endif
+
     return NGX_CONF_OK;
 }
 
--- a/src/http/ngx_http.c	Wed Jul 11 17:56:51 2018 +0300
+++ b/src/http/ngx_http.c	Thu Jul 12 19:50:02 2018 +0300
@@ -1685,10 +1685,6 @@
             break;
         }
 
-        if (ngx_clone_listening(cf, ls) != NGX_OK) {
-            return NGX_ERROR;
-        }
-
         addr++;
         last--;
     }
--- a/src/stream/ngx_stream.c	Wed Jul 11 17:56:51 2018 +0300
+++ b/src/stream/ngx_stream.c	Thu Jul 12 19:50:02 2018 +0300
@@ -538,10 +538,6 @@
                 break;
             }
 
-            if (ngx_clone_listening(cf, ls) != NGX_OK) {
-                return NGX_CONF_ERROR;
-            }
-
             addr++;
             last--;
         }