Skip to content

Commit da11870

Browse files
qleex001agentzh
authored andcommitted
bugfix: balancer_by_lua*: the number of retres might exceed the limit of proxy_next_upstream_tries or alike.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 5028d63 commit da11870

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ install:
7979
- git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
8080
- git clone https://github.com/openresty/srcache-nginx-module.git ../srcache-nginx-module
8181
- git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module
82+
- git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
8283
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
8384

8485
before_script:

src/ngx_http_lua_balancer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ ngx_http_lua_ffi_balancer_set_more_tries(ngx_http_request_t *r,
640640
int count, char **err)
641641
{
642642
#if (nginx_version >= 1007005)
643-
ngx_uint_t max_tries;
643+
ngx_uint_t max_tries, total;
644644
#endif
645645
ngx_http_lua_ctx_t *ctx;
646646
ngx_http_upstream_t *u;
@@ -681,9 +681,10 @@ ngx_http_lua_ffi_balancer_set_more_tries(ngx_http_request_t *r,
681681

682682
#if (nginx_version >= 1007005)
683683
max_tries = r->upstream->conf->next_upstream_tries;
684+
total = bp->total_tries + r->upstream->peer.tries - 1;
684685

685-
if (bp->total_tries + count > max_tries) {
686-
count = max_tries - bp->total_tries;
686+
if (max_tries && total + count > max_tries) {
687+
count = max_tries - total;
687688
*err = "reduced tries due to limit";
688689

689690
} else {

t/138-balancer.t

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,60 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\
431431
]
432432
--- no_error_log
433433
[warn]
434+
435+
436+
437+
=== TEST 15: test if execeed proxy_next_upstream_limit
438+
--- http_config
439+
lua_package_path "../lua-resty-core/lib/?.lua;;";
440+
441+
proxy_next_upstream_tries 5;
442+
upstream backend {
443+
server 0.0.0.1;
444+
balancer_by_lua_block {
445+
local b = require "ngx.balancer"
446+
447+
if not ngx.ctx.tries then
448+
ngx.ctx.tries = 0
449+
end
450+
451+
if ngx.ctx.tries >= 6 then
452+
ngx.log(ngx.ERR, "retry count exceed limit")
453+
ngx.exit(500)
454+
end
455+
456+
ngx.ctx.tries = ngx.ctx.tries + 1
457+
print("retry counter: ", ngx.ctx.tries)
458+
459+
local ok, err = b.set_more_tries(2)
460+
if not ok then
461+
return error("failed to set more tries: ", err)
462+
elseif err then
463+
ngx.log(ngx.WARN, "set more tries: ", err)
464+
end
465+
466+
assert(b.set_current_peer("127.0.0.1", 81))
467+
}
468+
}
469+
--- config
470+
location = /t {
471+
proxy_pass http://backend/back;
472+
}
473+
474+
location = /back {
475+
return 404;
476+
}
477+
--- request
478+
GET /t
479+
--- response_body_like: 502 Bad Gateway
480+
--- error_code: 502
481+
--- grep_error_log eval: qr/\bretry counter: \w+/
482+
--- grep_error_log_out
483+
retry counter: 1
484+
retry counter: 2
485+
retry counter: 3
486+
retry counter: 4
487+
retry counter: 5
488+
489+
--- error_log
490+
set more tries: reduced tries due to limit

0 commit comments

Comments
 (0)