From 8abf7a3402eb5333fff7994c2601b3a7d681b757 Mon Sep 17 00:00:00 2001 From: gavinhgchen Date: Wed, 15 Sep 2021 12:10:42 +0800 Subject: [PATCH] add config: max_tries that limits tries for request to backend ip --- README.md | 1 + .../ngx_http_upstream_polaris_module.cpp | 22 +++++++++++++++++-- .../ngx_http_upstream_polaris_module.h | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 680d949..ebe209f 100644 --- a/README.md +++ b/README.md @@ -179,4 +179,5 @@ upstream test_upstream { | mr | N | Switch for metadata route| off | | mr_mode | N | metadata route fail over mode | 0 | | fail_report | N | Business-level fail report status code | "" | +| max_tries | N | Limits the number of tries for request to backend ip | 2 | diff --git a/nginx_polaris_module/ngx_http_upstream_polaris_module.cpp b/nginx_polaris_module/ngx_http_upstream_polaris_module.cpp index ba02c8a..0170274 100644 --- a/nginx_polaris_module/ngx_http_upstream_polaris_module.cpp +++ b/nginx_polaris_module/ngx_http_upstream_polaris_module.cpp @@ -205,6 +205,9 @@ static ngx_int_t ngx_http_upstream_init_polaris_peer(ngx_http_request_t *r, r->upstream->peer.get = ngx_http_upstream_get_polaris_peer; r->upstream->peer.free = ngx_http_upstream_free_polaris_peer; + if (dcf->max_tries != NGX_CONF_UNSET_UINT) { + r->upstream->peer.tries = dcf->max_tries; + } // control the retry times >= 2. if (r->upstream->peer.tries < 2) { r->upstream->peer.tries = 2; @@ -340,6 +343,7 @@ static void *ngx_http_upstream_polaris_create_conf(ngx_conf_t *cf) { conf->polaris_metadata_route_enabled = false; ngx_str_set(&conf->polaris_fail_status_list, ""); conf->polaris_fail_status_report_enabled = false; + conf->max_tries = NGX_CONF_UNSET_UINT; return conf; } @@ -584,6 +588,20 @@ static char *ngx_http_upstream_polaris_set_handler(ngx_conf_t *cf, ngx_command_t continue; } + + if (ngx_strncmp(value[i].data, "max_tries=", 10) == 0) { + ngx_str_t s = {value[i].len - 10, &value[i].data[10]}; + + ngx_int_t max_tries = ngx_atoi(s.data, s.len); + if (max_tries < 1 || max_tries > 256) { + ngx_conf_log_error(NGX_LOG_ERR, cf, 0, + "dcf->max_tries:%d invalid, only valid in (1-256)", + max_tries); + return const_cast("invalid polaris max_tries"); + } + dcf->max_tries = max_tries; + continue; + } } dcf->enabled = true; @@ -645,10 +663,10 @@ static char *ngx_http_upstream_polaris_set_handler(ngx_conf_t *cf, ngx_command_t ngx_conf_log_error( NGX_LOG_NOTICE, cf, 0, "init service_namespace:%s, service_name:%s, timeout: %.2f, mode: %d, " - "key: %s, dr: %d, mr_mode: %d, fail_status: %s", + "key: %s, dr: %d, mr_mode: %d, fail_status: %s, max_tries: %d", dcf->polaris_service_namespace.data, dcf->polaris_service_name.data, dcf->polaris_timeout, dcf->polaris_lb_mode, dcf->polaris_lb_key.data, dcf->polaris_dynamic_route_enabled, - dcf->metadata_route_failover_mode, dcf->polaris_fail_status_list.data); + dcf->metadata_route_failover_mode, dcf->polaris_fail_status_list.data, dcf->max_tries); return NGX_CONF_OK; } diff --git a/nginx_polaris_module/ngx_http_upstream_polaris_module.h b/nginx_polaris_module/ngx_http_upstream_polaris_module.h index 941a93b..1d246c8 100644 --- a/nginx_polaris_module/ngx_http_upstream_polaris_module.h +++ b/nginx_polaris_module/ngx_http_upstream_polaris_module.h @@ -62,6 +62,8 @@ typedef struct { ngx_http_upstream_init_pt original_init_upstream; ngx_http_upstream_init_peer_pt original_init_peer; + + ngx_uint_t max_tries; } ngx_http_upstream_polaris_srv_conf_t; /**