Skip to content

Commit

Permalink
implemented nobackup upstream singlets
Browse files Browse the repository at this point in the history
  • Loading branch information
lyokha committed Jul 14, 2023
1 parent f492550 commit 5f6d671
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
nginx: [1.16.1, 1.18.0, 1.20.2, 1.22.1]
nginx: [1.16.1, 1.18.0, 1.20.2, 1.22.1, 1.24.0]
experimental: [false]
include:
- nginx: head
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ adjust their names. The 1st parameter is a suffix added after the name of the
host upstream and before the ordering number. The 2nd parameter must be an
integer value which defines *zero-alignment* of the ordering number, for example
if it has value 2 then the ordering numbers could be
``'01', '02', ..., '10', ... '100' ...``
``'01', '02', ..., '10', ... '100' ...``. There is another optional parameter
*nobackup* which marks secondary servers as down rather than backup. This
parameter must be put after all other parameters.

### An example

Expand Down
14 changes: 14 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ http {
combine_server_singlets byname;
combine_server_singlets _tmp_ 2;
}
upstream u4 {
server localhost:8020;
server localhost:8030;
combine_server_singlets _single_ nobackup;
}

upstream u01 {
# to test next_upstream_statuses error set port 8140
Expand Down Expand Up @@ -67,6 +72,12 @@ http {
next_upstream_statuses 5xx;
intercept_statuses 5xx /Internal/failover;
}
upstrand us4 {
upstream ~^u4_single_ blacklist_interval=60s;
order per_request;
next_upstream_statuses 5xx;
intercept_statuses 5xx /Internal/failover;
}

proxy_read_timeout 5s;
proxy_intercept_errors on;
Expand Down Expand Up @@ -106,6 +117,9 @@ http {
proxy_buffering off;
proxy_pass http://$upstrand_us2;
}
location /us4 {
proxy_pass http://$upstrand_us4;
}
location /echo/us1 {
echo $upstrand_us1;
}
Expand Down
29 changes: 23 additions & 6 deletions src/ngx_http_combined_upstreams_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static ngx_command_t ngx_http_combined_upstreams_commands[] = {
0,
NULL },
{ ngx_string("combine_server_singlets"),
NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE12,
NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE123,
ngx_http_combine_server_singlets,
0,
0,
Expand Down Expand Up @@ -325,7 +325,7 @@ ngx_http_combine_server_singlets(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
u_char *fbuf;
const char *fmt = "%V%d";
ngx_uint_t flen;
ngx_uint_t byname = 0;
ngx_uint_t byname = 0, nobackup = 0;

uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
if (uscf->servers == NULL) {
Expand All @@ -337,6 +337,19 @@ ngx_http_combine_server_singlets(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
value = cf->args->elts;

if (cf->args->nelts > 1) {
if (value[cf->args->nelts - 1].len == 8
&& ngx_strncmp(value[cf->args->nelts - 1].data, "nobackup", 8) == 0)
{
nobackup = 1;
}
}

if (cf->args->nelts > 3 && nobackup == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameters");
return NGX_CONF_ERROR;
}

if (cf->args->nelts > 1 + nobackup) {
suf = value[1];

#if nginx_version >= 1007002
Expand All @@ -348,10 +361,10 @@ ngx_http_combine_server_singlets(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}
#endif

if (cf->args->nelts > 2) {
if (cf->args->nelts > 2 + nobackup) {
if (byname > 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"second parameter is not permitted when "
"setting field width is not permitted when "
"using \"byname\" value");
return NGX_CONF_ERROR;
}
Expand All @@ -367,7 +380,7 @@ ngx_http_combine_server_singlets(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
if (ngx_atoi(value[2].data, value[2].len) == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"second parameter \"%V\" must be an "
"field width \"%V\" must be an "
"integer value", &value[2]);
return NGX_CONF_ERROR;
}
Expand Down Expand Up @@ -463,7 +476,11 @@ ngx_http_combine_server_singlets(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_memcpy(usn, uscf->servers->elts,
sizeof(ngx_http_upstream_server_t) * uscf->servers->nelts);
for (j = 0; j < uscf->servers->nelts; ++j) {
usn[j].backup = i == j ? 0 : 1;
if (nobackup) {
usn[j].down = i == j ? usn[j].down : 1;
} else {
usn[j].backup = i == j ? 0 : 1;
}
}

/*ngx_conf_log_error(NGX_LOG_ERR, cf, 0,*/
Expand Down
23 changes: 22 additions & 1 deletion test/t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Test::Nginx::Socket;

repeat_each(2);
plan tests => repeat_each() * (2 * (blocks() + 2));
plan tests => repeat_each() * (2 * (blocks() + 3));

no_shuffle();
run_tests();
Expand All @@ -30,6 +30,11 @@ __DATA__
combine_server_singlets byname;
combine_server_singlets _tmp_ 2;
}
upstream u4 {
server localhost:8020;
server localhost:8030;
combine_server_singlets _single_ nobackup;
}

upstream u01 {
server localhost:8040;
Expand Down Expand Up @@ -59,6 +64,12 @@ __DATA__
next_upstream_statuses error timeout 5xx;
intercept_statuses 5xx /Internal/failover;
}
upstrand us4 {
upstream ~^u4_single_ blacklist_interval=60s;
order per_request;
next_upstream_statuses 5xx;
intercept_statuses 5xx /Internal/failover;
}

proxy_read_timeout 5s;
proxy_intercept_errors on;
Expand Down Expand Up @@ -136,6 +147,9 @@ __DATA__
location /us3 {
proxy_pass http://$upstrand_us3;
}
location /us4 {
proxy_pass http://$upstrand_us4;
}
location /echo/us1 {
echo $upstrand_us1;
}
Expand Down Expand Up @@ -260,3 +274,10 @@ sub {
In 8060
--- error_code: 200

=== TEST 11: combined upstreams singlets in upstrand with no round-robin
--- request eval
["GET /us4", "GET /us4"]
--- response_body eval
["Passed to backend1\n", "Passed to backend1\n"]
--- error_code eval: [200, 200]

0 comments on commit 5f6d671

Please sign in to comment.