Skip to content

Commit

Permalink
allow attribute weight=N in directive add_upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
lyokha committed Nov 6, 2023
1 parent 8934cdf commit 5df6618
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ Directive add_upstream
----------------------

Populates the host upstream with servers listed in an already defined upstream
specified by the mandatory 1st parameter of the directive. Optional 2nd
parameter may have only value *backup* which marks all servers of the sourced
upstream as backup.
specified by the mandatory 1st parameter of the directive. The server attributes
such as weights, max_fails and others are kept in the host upstream. Optional
parameters may include values *backup* to mark all servers of the sourced
upstream as backup servers and *weight=N* to calibrate weights of servers of the
sourced upstream by multiplying them by factor *N*.

### An example

Expand Down
31 changes: 27 additions & 4 deletions src/ngx_http_combined_upstreams_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static ngx_int_t ngx_http_upstream_init_extend_single_peers(ngx_conf_t *cf,
static ngx_command_t ngx_http_combined_upstreams_commands[] = {

{ ngx_string("add_upstream"),
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE12,
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE123,
ngx_http_add_upstream,
0,
0,
Expand Down Expand Up @@ -257,6 +257,7 @@ ngx_http_add_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_http_upstream_server_t *us;
ngx_str_t *value;
ngx_uint_t backup = 0;
ngx_int_t weight = 0;

umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
Expand All @@ -270,12 +271,29 @@ ngx_http_add_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}

if (cf->args->nelts == 3) {
if (ngx_strncmp(value[2].data, "backup", 6) == 0) {
for (i = 2; i < cf->args->nelts; i++) {
if (ngx_strncmp(value[i].data, "backup", 6) == 0) {
if (backup) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"parameter \"backup\" has been already declared");
return NGX_CONF_ERROR;
}
backup = 1;
} else if (ngx_strncmp(value[i].data, "weight=", 7) == 0) {
if (weight) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"parameter \"weight\" has been already declared");
return NGX_CONF_ERROR;
}
weight = ngx_atoi(value[i].data + 7, value[i].len - 7);
if (weight < 1) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"parameter \"weight\" must be a positive integer value");
return NGX_CONF_ERROR;
}
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
&value[2]);
&value[i]);
return NGX_CONF_ERROR;
}
}
Expand Down Expand Up @@ -303,6 +321,11 @@ ngx_http_add_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
us[j].backup = 1;
}
}
if (weight) {
for (j = 0; j < uscfp[i]->servers->nelts; j++) {
us[j].weight *= weight;
}
}

return NGX_CONF_OK;
}
Expand Down

0 comments on commit 5df6618

Please sign in to comment.