Permalink
Browse files

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>
  • Loading branch information...
1 parent 5028d63 commit da11870db65eb827ae00c0b24255ba749aefd060 @qlee001 qlee001 committed with agentzh Nov 14, 2016
Showing with 62 additions and 3 deletions.
  1. +1 −0 .travis.yml
  2. +4 −3 src/ngx_http_lua_balancer.c
  3. +57 −0 t/138-balancer.t
View
@@ -79,6 +79,7 @@ install:
- git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
- git clone https://github.com/openresty/srcache-nginx-module.git ../srcache-nginx-module
- git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module
+ - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
before_script:
@@ -640,7 +640,7 @@ ngx_http_lua_ffi_balancer_set_more_tries(ngx_http_request_t *r,
int count, char **err)
{
#if (nginx_version >= 1007005)
- ngx_uint_t max_tries;
+ ngx_uint_t max_tries, total;
#endif
ngx_http_lua_ctx_t *ctx;
ngx_http_upstream_t *u;
@@ -681,9 +681,10 @@ ngx_http_lua_ffi_balancer_set_more_tries(ngx_http_request_t *r,
#if (nginx_version >= 1007005)
max_tries = r->upstream->conf->next_upstream_tries;
+ total = bp->total_tries + r->upstream->peer.tries - 1;
- if (bp->total_tries + count > max_tries) {
- count = max_tries - bp->total_tries;
+ if (max_tries && total + count > max_tries) {
+ count = max_tries - total;
*err = "reduced tries due to limit";
} else {
View
@@ -431,3 +431,60 @@ qr{\[crit\] .*? connect\(\) to 0\.0\.0\.1:80 failed .*?, upstream: "http://0\.0\
]
--- no_error_log
[warn]
+
+
+
+=== TEST 15: test if execeed proxy_next_upstream_limit
+--- http_config
+ lua_package_path "../lua-resty-core/lib/?.lua;;";
+
+ proxy_next_upstream_tries 5;
+ upstream backend {
+ server 0.0.0.1;
+ balancer_by_lua_block {
+ local b = require "ngx.balancer"
+
+ if not ngx.ctx.tries then
+ ngx.ctx.tries = 0
+ end
+
+ if ngx.ctx.tries >= 6 then
+ ngx.log(ngx.ERR, "retry count exceed limit")
+ ngx.exit(500)
+ end
+
+ ngx.ctx.tries = ngx.ctx.tries + 1
+ print("retry counter: ", ngx.ctx.tries)
+
+ local ok, err = b.set_more_tries(2)
+ if not ok then
+ return error("failed to set more tries: ", err)
+ elseif err then
+ ngx.log(ngx.WARN, "set more tries: ", err)
+ end
+
+ assert(b.set_current_peer("127.0.0.1", 81))
+ }
+ }
+--- config
+ location = /t {
+ proxy_pass http://backend/back;
+ }
+
+ location = /back {
+ return 404;
+ }
+--- request
+ GET /t
+--- response_body_like: 502 Bad Gateway
+--- error_code: 502
+--- grep_error_log eval: qr/\bretry counter: \w+/
+--- grep_error_log_out
+retry counter: 1
+retry counter: 2
+retry counter: 3
+retry counter: 4
+retry counter: 5
+
+--- error_log
+set more tries: reduced tries due to limit

0 comments on commit da11870

Please sign in to comment.