[PATCH] Core: configurable listening try number

2014-04-07 Thread Han Cheng

Hello, there,

I'm a new guy to nginx.
Recently, I'm reading the source code. I found some TODOs. I'm trying to
implement some. This is my first small attempt.

Any suggestion?

Thank you!

Regards,
Cheng


# HG changeset patch
# User Han Cheng 
# Date 1396856176 -28800
#  Mon Apr 07 15:36:16 2014 +0800
# Node ID de04aae10531d3bae43804231ed3566ececec481
# Parent  0c0dd1aacdf55f3422cfa2edd4dfe85f4d0d8b34
Core: configurable listening try number

diff -r 0c0dd1aacdf5 -r de04aae10531 src/core/nginx.c
--- a/src/core/nginx.c  Tue Apr 01 20:53:18 2014 +0400
+++ b/src/core/nginx.c  Mon Apr 07 15:36:16 2014 +0800
@@ -23,6 +23,8 @@
 void *conf);
 static char *ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd,
 void *conf);
+static char *ngx_set_listening_tries(ngx_conf_t *cf, ngx_command_t *cmd,
+void *conf);
 
 
 static ngx_conf_enum_t  ngx_debug_points[] = {
@@ -83,6 +85,13 @@
   offsetof(ngx_core_conf_t, debug_points),
   &ngx_debug_points },
 
+{ ngx_string("listening_tries"),
+  NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
+  ngx_set_listening_tries,
+  0,
+  0,
+  NULL },
+
 { ngx_string("user"),
   NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
   ngx_set_user,
@@ -952,6 +961,8 @@
 ccf->worker_processes = NGX_CONF_UNSET;
 ccf->debug_points = NGX_CONF_UNSET;
 
+ccf->listening_tries = NGX_CONF_UNSET;
+
 ccf->rlimit_nofile = NGX_CONF_UNSET;
 ccf->rlimit_core = NGX_CONF_UNSET;
 ccf->rlimit_sigpending = NGX_CONF_UNSET;
@@ -986,6 +997,8 @@
 ngx_conf_init_value(ccf->worker_processes, 1);
 ngx_conf_init_value(ccf->debug_points, 0);
 
+ngx_conf_init_value(ccf->listening_tries, 5);
+
 #if (NGX_HAVE_CPU_AFFINITY)
 
 if (ccf->cpu_affinity_n
@@ -1361,3 +1374,29 @@
 
 return NGX_CONF_OK;
 }
+
+
+static char *
+ngx_set_listening_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->listening_tries != NGX_CONF_UNSET) {
+return "is duplicate";
+}
+
+value = (ngx_str_t *) cf->args->elts;
+
+ccf->listening_tries = ngx_atoi(value[1].data, value[1].len);
+
+if (ccf->listening_tries == NGX_ERROR) {
+return "invalid value";
+} else if (ccf->listening_tries > 1024) {
+return "too much tries";
+}
+
+return NGX_CONF_OK;
+}
diff -r 0c0dd1aacdf5 -r de04aae10531 src/core/ngx_connection.c
--- a/src/core/ngx_connection.c Tue Apr 01 20:53:18 2014 +0400
+++ b/src/core/ngx_connection.c Mon Apr 07 15:36:16 2014 +0800
@@ -305,11 +305,13 @@
 ngx_open_listening_sockets(ngx_cycle_t *cycle)
 {
 int   reuseaddr;
-ngx_uint_ti, tries, failed;
+ngx_uint_ti, failed;
+ngx_int_t tries;
 ngx_err_t err;
 ngx_log_t*log;
 ngx_socket_t  s;
 ngx_listening_t  *ls;
+ngx_core_conf_t  *ccf;
 
 reuseaddr = 1;
 #if (NGX_SUPPRESS_WARN)
@@ -318,9 +320,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->listening_tries; tries; tries--) {
 failed = 0;
 
 /* for each listening socket */
diff -r 0c0dd1aacdf5 -r de04aae10531 src/core/ngx_cycle.h
--- a/src/core/ngx_cycle.h  Tue Apr 01 20:53:18 2014 +0400
+++ b/src/core/ngx_cycle.h  Mon Apr 07 15:36:16 2014 +0800
@@ -81,6 +81,8 @@
  ngx_int_tworker_processes;
  ngx_int_tdebug_points;
 
+ ngx_int_tlistening_tries;
+
  ngx_int_trlimit_nofile;
  ngx_int_trlimit_sigpending;
  off_trlimit_core;


___
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel


Re: [PATCH] Core: configurable listening try number

2014-04-07 Thread Han Cheng
Hello, Maxim,

As I'm new to nginx, please point out if I'm wrong.

The reason why we should have multiply tries is we may fail when we bind().
That is because the address and port
are in use(temporary which  caused by unclean close or others, permanent
like another nginx is using) or others.

In the temporary use case, we may wait and retry. Configurable retry make
us more probable succeed in that case.

By the way, one other TODO in the source code is the configurable wait
time. I think we do not need it when we have
retry.

Anyway, in most cases, we succeed in the first try and go on.

Thanks


2014-04-07 21:46 GMT+08:00 Maxim Dounin :

> Hello!
>
> On Mon, Apr 07, 2014 at 04:34:52PM +0800, Han Cheng wrote:
>
> >
> > Hello, there,
> >
> > I'm a new guy to nginx.
> > Recently, I'm reading the source code. I found some TODOs. I'm trying to
> > implement some. This is my first small attempt.
> >
> > Any suggestion?
>
> It doesn't looks like it actually needs to be configurable.  I
> don't recall any single request to make this configurable, nor any
> single use case where it may help.
>
> --
> Maxim Dounin
> http://nginx.org/
>
> ___
> nginx-devel mailing list
> nginx-devel@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel
>



-- 
Best regards,
Cheng
___
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel