[PATCH 1 of 2] Added functions for starting and signaling a single worker

Roman Arutyunyan arut at nginx.com
Mon Aug 28 12:30:43 UTC 2023


# HG changeset patch
# User Roman Arutyunyan <arut at nginx.com>
# Date 1692694526 -14400
#      Tue Aug 22 12:55:26 2023 +0400
# Node ID fdad00808cb485ab83e919be59e9211ef06ac56a
# Parent  58afcd72446ff33811e773f1cabb7866a92a09a0
Added functions for starting and signaling a single worker.

The functions are ngx_start_worker_process() and ngx_signal_worker_process().
This change is a preparation for limiting the number of shutdown workers while
reloading nginx configuration.

diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -13,10 +13,14 @@
 
 static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n,
     ngx_int_t type);
+static void ngx_start_worker_process(ngx_cycle_t *cycle, ngx_int_t i,
+    ngx_int_t type);
 static void ngx_start_cache_manager_processes(ngx_cycle_t *cycle,
     ngx_uint_t respawn);
 static void ngx_pass_open_channel(ngx_cycle_t *cycle);
 static void ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo);
+static void ngx_signal_worker_process(ngx_cycle_t *cycle, ngx_int_t i,
+    int signo);
 static ngx_uint_t ngx_reap_children(ngx_cycle_t *cycle);
 static void ngx_master_process_exit(ngx_cycle_t *cycle);
 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
@@ -340,12 +344,18 @@ ngx_start_worker_processes(ngx_cycle_t *
     ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start worker processes");
 
     for (i = 0; i < n; i++) {
+        ngx_start_worker_process(cycle, i, type);
+    }
+}
 
-        ngx_spawn_process(cycle, ngx_worker_process_cycle,
-                          (void *) (intptr_t) i, "worker process", type);
 
-        ngx_pass_open_channel(cycle);
-    }
+static void
+ngx_start_worker_process(ngx_cycle_t *cycle, ngx_int_t i, ngx_int_t type)
+{
+    ngx_spawn_process(cycle, ngx_worker_process_cycle,
+                      (void *) (intptr_t) i, "worker process", type);
+
+    ngx_pass_open_channel(cycle);
 }
 
 
@@ -431,7 +441,17 @@ ngx_pass_open_channel(ngx_cycle_t *cycle
 static void
 ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo)
 {
-    ngx_int_t      i;
+    ngx_int_t  i;
+
+    for (i = 0; i < ngx_last_process; i++) {
+        ngx_signal_worker_process(cycle, i, signo);
+    }
+}
+
+
+static void
+ngx_signal_worker_process(ngx_cycle_t *cycle, ngx_int_t i, int signo)
+{
     ngx_err_t      err;
     ngx_channel_t  ch;
 
@@ -466,66 +486,63 @@ ngx_signal_worker_processes(ngx_cycle_t 
     ch.fd = -1;
 
 
-    for (i = 0; i < ngx_last_process; i++) {
+    ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
+                   "child: %i %P e:%d t:%d d:%d r:%d j:%d",
+                   i,
+                   ngx_processes[i].pid,
+                   ngx_processes[i].exiting,
+                   ngx_processes[i].exited,
+                   ngx_processes[i].detached,
+                   ngx_processes[i].respawn,
+                   ngx_processes[i].just_spawn);
 
-        ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                       "child: %i %P e:%d t:%d d:%d r:%d j:%d",
-                       i,
-                       ngx_processes[i].pid,
-                       ngx_processes[i].exiting,
-                       ngx_processes[i].exited,
-                       ngx_processes[i].detached,
-                       ngx_processes[i].respawn,
-                       ngx_processes[i].just_spawn);
+    if (ngx_processes[i].detached || ngx_processes[i].pid == -1) {
+        return;
+    }
+
+    if (ngx_processes[i].just_spawn) {
+        ngx_processes[i].just_spawn = 0;
+        return;
+    }
+
+    if (ngx_processes[i].exiting
+        && signo == ngx_signal_value(NGX_SHUTDOWN_SIGNAL))
+    {
+        return;
+    }
 
-        if (ngx_processes[i].detached || ngx_processes[i].pid == -1) {
-            continue;
+    if (ch.command) {
+        if (ngx_write_channel(ngx_processes[i].channel[0],
+                              &ch, sizeof(ngx_channel_t), cycle->log)
+            == NGX_OK)
+        {
+            if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
+                ngx_processes[i].exiting = 1;
+            }
+
+            return;
         }
+    }
 
-        if (ngx_processes[i].just_spawn) {
-            ngx_processes[i].just_spawn = 0;
-            continue;
-        }
+    ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
+                   "kill (%P, %d)", ngx_processes[i].pid, signo);
 
-        if (ngx_processes[i].exiting
-            && signo == ngx_signal_value(NGX_SHUTDOWN_SIGNAL))
-        {
-            continue;
+    if (kill(ngx_processes[i].pid, signo) == -1) {
+        err = ngx_errno;
+        ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
+                      "kill(%P, %d) failed", ngx_processes[i].pid, signo);
+
+        if (err == NGX_ESRCH) {
+            ngx_processes[i].exited = 1;
+            ngx_processes[i].exiting = 0;
+            ngx_reap = 1;
         }
 
-        if (ch.command) {
-            if (ngx_write_channel(ngx_processes[i].channel[0],
-                                  &ch, sizeof(ngx_channel_t), cycle->log)
-                == NGX_OK)
-            {
-                if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
-                    ngx_processes[i].exiting = 1;
-                }
-
-                continue;
-            }
-        }
-
-        ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                       "kill (%P, %d)", ngx_processes[i].pid, signo);
+        return;
+    }
 
-        if (kill(ngx_processes[i].pid, signo) == -1) {
-            err = ngx_errno;
-            ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
-                          "kill(%P, %d) failed", ngx_processes[i].pid, signo);
-
-            if (err == NGX_ESRCH) {
-                ngx_processes[i].exited = 1;
-                ngx_processes[i].exiting = 0;
-                ngx_reap = 1;
-            }
-
-            continue;
-        }
-
-        if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
-            ngx_processes[i].exiting = 1;
-        }
+    if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
+        ngx_processes[i].exiting = 1;
     }
 }
 


More information about the nginx-devel mailing list