[PATCH] Add the configurable try number for binding

Xingyuan winshining at 163.com
Mon Apr 17 02:56:50 UTC 2017


# HG changeset patch
# User Xingyuan <winshining at 163.com>
# Date 1492252474 -28800
# Node ID 302e24fbe01d2e20bc8519d9dad71acee6540530
# Parent  be5cfa918bfc07f378c8f905e5e9349f1825c268
Add the configurable try number for binding.

The try number of binding now can be specified in nginx.conf, for example:

bind_tries  20;

With it, there can be enough time that when the new nginx process is being
started while the old one is still running, we can do something, kill it
for example, instead of waiting for a so short time that new one exceeds
the try number and exits.

diff -r be5cfa918bfc -r 302e24fbe01d src/core/nginx.c
--- a/src/core/nginx.c	Tue Apr 11 03:13:46 2017 +0200
+++ b/src/core/nginx.c	Sat Apr 15 18:34:34 2017 +0800
@@ -25,6 +25,8 @@
     void *conf);
 static char *ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
+static char *ngx_set_bind_tries(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
 static char *ngx_load_module(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 #if (NGX_HAVE_DLOPEN)
 static void ngx_unload_module(void *data);
@@ -82,6 +84,13 @@
       0,
       NULL },
 
+    { ngx_string("bind_tries"),
+      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
+      ngx_set_bind_tries,
+      0,
+      0,
+      NULL },
+
     { ngx_string("debug_points"),
       NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_enum_slot,
@@ -1029,6 +1038,8 @@
     ccf->rlimit_nofile = NGX_CONF_UNSET;
     ccf->rlimit_core = NGX_CONF_UNSET;
 
+    ccf->bind_tries = NGX_CONF_UNSET;
+
     ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
     ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;
 
@@ -1055,6 +1066,8 @@
     ngx_conf_init_value(ccf->worker_processes, 1);
     ngx_conf_init_value(ccf->debug_points, 0);
 
+    ngx_conf_init_value(ccf->bind_tries, 5);
+
 #if (NGX_HAVE_CPU_AFFINITY)
 
     if (!ccf->cpu_affinity_auto
@@ -1486,6 +1499,37 @@
 
 
 static char *
+ngx_set_bind_tries(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_str_t        *value;
+    ngx_core_conf_t  *ccf;
+
+    ccf = (ngx_core_conf_t *) conf;
+
+    if (ccf->bind_tries != NGX_CONF_UNSET) {
+        return "is duplicate";
+    }
+
+    value = cf->args->elts;
+
+    ccf->bind_tries = ngx_atoi(value[1].data, value[1].len);
+
+    if (ccf->bind_tries == NGX_ERROR) {
+        return "invalid value";
+    }
+
+    if (ccf->bind_tries <= 0) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                     "\"bind_tries\" directive has an invalid value %d",
+                     ccf->bind_tries);
+        return "invalid value";
+    }
+
+    return NGX_CONF_OK;
+}
+
+
+static char *
 ngx_load_module(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
 #if (NGX_HAVE_DLOPEN)
diff -r be5cfa918bfc -r 302e24fbe01d src/core/ngx_connection.c
--- a/src/core/ngx_connection.c	Tue Apr 11 03:13:46 2017 +0200
+++ b/src/core/ngx_connection.c	Sat Apr 15 18:34:34 2017 +0800
@@ -386,6 +386,7 @@
     ngx_log_t        *log;
     ngx_socket_t      s;
     ngx_listening_t  *ls;
+    ngx_core_conf_t  *ccf;
 
     reuseaddr = 1;
 #if (NGX_SUPPRESS_WARN)
@@ -394,9 +395,9 @@
 
     log = cycle->log;
 
-    /* TODO: configurable try number */
+    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-    for (tries = 5; tries; tries--) {
+    for (tries = ccf->bind_tries; tries; tries--) {
         failed = 0;
 
         /* for each listening socket */
diff -r be5cfa918bfc -r 302e24fbe01d src/core/ngx_cycle.h
--- a/src/core/ngx_cycle.h	Tue Apr 11 03:13:46 2017 +0200
+++ b/src/core/ngx_cycle.h	Sat Apr 15 18:34:34 2017 +0800
@@ -96,6 +96,8 @@
     ngx_int_t                 rlimit_nofile;
     off_t                     rlimit_core;
 
+    ngx_int_t                 bind_tries;
+
     int                       priority;
 
     ngx_uint_t                cpu_affinity_auto;


More information about the nginx-devel mailing list