Skip to content

Commit 8a8e3f0

Browse files
authored
Merge pull request #2 from chen-honggang/dev/add-max-tries
add config: max_tries that limits tries for request to backend ip
2 parents 8c99dc9 + 8abf7a3 commit 8a8e3f0

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,5 @@ upstream test_upstream {
179179
| mr | N | Switch for metadata route| off |
180180
| mr_mode | N | metadata route fail over mode | 0 |
181181
| fail_report | N | Business-level fail report status code | "" |
182+
| max_tries | N | Limits the number of tries for request to backend ip | 2 |
182183

nginx_polaris_module/ngx_http_upstream_polaris_module.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ static ngx_int_t ngx_http_upstream_init_polaris_peer(ngx_http_request_t *r,
205205
r->upstream->peer.get = ngx_http_upstream_get_polaris_peer;
206206
r->upstream->peer.free = ngx_http_upstream_free_polaris_peer;
207207

208+
if (dcf->max_tries != NGX_CONF_UNSET_UINT) {
209+
r->upstream->peer.tries = dcf->max_tries;
210+
}
208211
// control the retry times >= 2.
209212
if (r->upstream->peer.tries < 2) {
210213
r->upstream->peer.tries = 2;
@@ -340,6 +343,7 @@ static void *ngx_http_upstream_polaris_create_conf(ngx_conf_t *cf) {
340343
conf->polaris_metadata_route_enabled = false;
341344
ngx_str_set(&conf->polaris_fail_status_list, "");
342345
conf->polaris_fail_status_report_enabled = false;
346+
conf->max_tries = NGX_CONF_UNSET_UINT;
343347

344348
return conf;
345349
}
@@ -584,6 +588,20 @@ static char *ngx_http_upstream_polaris_set_handler(ngx_conf_t *cf, ngx_command_t
584588

585589
continue;
586590
}
591+
592+
if (ngx_strncmp(value[i].data, "max_tries=", 10) == 0) {
593+
ngx_str_t s = {value[i].len - 10, &value[i].data[10]};
594+
595+
ngx_int_t max_tries = ngx_atoi(s.data, s.len);
596+
if (max_tries < 1 || max_tries > 256) {
597+
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
598+
"dcf->max_tries:%d invalid, only valid in (1-256)",
599+
max_tries);
600+
return const_cast<char *>("invalid polaris max_tries");
601+
}
602+
dcf->max_tries = max_tries;
603+
continue;
604+
}
587605
}
588606

589607
dcf->enabled = true;
@@ -645,10 +663,10 @@ static char *ngx_http_upstream_polaris_set_handler(ngx_conf_t *cf, ngx_command_t
645663
ngx_conf_log_error(
646664
NGX_LOG_NOTICE, cf, 0,
647665
"init service_namespace:%s, service_name:%s, timeout: %.2f, mode: %d, "
648-
"key: %s, dr: %d, mr_mode: %d, fail_status: %s",
666+
"key: %s, dr: %d, mr_mode: %d, fail_status: %s, max_tries: %d",
649667
dcf->polaris_service_namespace.data, dcf->polaris_service_name.data, dcf->polaris_timeout,
650668
dcf->polaris_lb_mode, dcf->polaris_lb_key.data, dcf->polaris_dynamic_route_enabled,
651-
dcf->metadata_route_failover_mode, dcf->polaris_fail_status_list.data);
669+
dcf->metadata_route_failover_mode, dcf->polaris_fail_status_list.data, dcf->max_tries);
652670

653671
return NGX_CONF_OK;
654672
}

nginx_polaris_module/ngx_http_upstream_polaris_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ typedef struct {
6262

6363
ngx_http_upstream_init_pt original_init_upstream;
6464
ngx_http_upstream_init_peer_pt original_init_peer;
65+
66+
ngx_uint_t max_tries;
6567
} ngx_http_upstream_polaris_srv_conf_t;
6668

6769
/**

0 commit comments

Comments
 (0)