Skip to content

Commit

Permalink
bugfix plugin: used config's version as a part of key. (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
membphis authored and moonming committed Jul 3, 2019
1 parent bf52796 commit a626bde
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lua/apisix/plugins/limit-conn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function _M.access(conf, ctx)
return 500
end

local key = ctx.var[conf.key]
local key = (ctx.var[conf.key] or "") .. ctx.conf_version
local rejected_code = conf.rejected_code

local delay, err = lim:incoming(key, true)
Expand Down
2 changes: 1 addition & 1 deletion lua/apisix/plugins/limit-count.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function _M.access(conf, ctx)
return 500
end

local key = ctx.var[conf.key]
local key = (ctx.var[conf.key] or "") .. ctx.conf_version
local rejected_code = conf.rejected_code

local delay, remaining = lim:incoming(key, true)
Expand Down
2 changes: 1 addition & 1 deletion lua/apisix/plugins/limit-req.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function _M.access(conf, ctx)
return 500
end

local key = ctx.var[conf.key]
local key = (ctx.var[conf.key] or "") .. ctx.conf_version
local delay, err = lim:incoming(key, true)
if not delay then
if err == "rejected" then
Expand Down
84 changes: 78 additions & 6 deletions t/plugin/limit-conn.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
BEGIN {
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
$SkipReason = "unavailable for the hup tests";

} else {
$ENV{TEST_NGINX_USE_HUP} = 1;
undef $ENV{TEST_NGINX_USE_STAP};
}
}

use t::APISix 'no_plan';

repeat_each(1);
Expand Down Expand Up @@ -263,7 +273,69 @@ GET /test_concurrency
=== TEST 7: invalid route: missing key
=== TEST 7: update plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"limit-conn": {
"conn": 5,
"burst": 1,
"default_conn_delay": 0.1,
"rejected_code": 503,
"key": "remote_addr"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/limit_conn"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 8: exceeding the burst
--- request
GET /test_concurrency
--- timeout: 10s
--- response_body
200
200
200
200
200
200
503
503
503
503
--- no_error_log
[error]
=== TEST 9: invalid route: missing key
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -305,7 +377,7 @@ GET /t
=== TEST 8: invalid route: wrong conn
=== TEST 10: invalid route: wrong conn
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -348,7 +420,7 @@ GET /t
=== TEST 9: invalid service: missing key
=== TEST 11: invalid service: missing key
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -389,7 +461,7 @@ GET /t
=== TEST 10: invalid service: wrong count
=== TEST 12: invalid service: wrong count
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -431,7 +503,7 @@ GET /t
=== TEST 11: disable plugin
=== TEST 13: disable plugin
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -466,7 +538,7 @@ passed
=== TEST 12: exceeding the burst
=== TEST 14: exceeding the burst
--- request
GET /test_concurrency
--- timeout: 10s
Expand Down
84 changes: 73 additions & 11 deletions t/plugin/limit-count.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
BEGIN {
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
$SkipReason = "unavailable for the hup tests";

} else {
$ENV{TEST_NGINX_USE_HUP} = 1;
undef $ENV{TEST_NGINX_USE_STAP};
}
}

use t::APISix 'no_plan';

repeat_each(1);
Expand Down Expand Up @@ -109,13 +119,65 @@ passed
--- pipelined_requests eval
["GET /hello1", "GET /hello", "GET /hello2", "GET /hello", "GET /hello"]
--- error_code eval
[404, 200, 404, 200, 503]
[404, 503, 404, 503, 503]
--- no_error_log
[error]



=== TEST 6: invalid route: missing key
=== TEST 6: set route(id: 1)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"plugins": {
"limit-count": {
"count": 3,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 7: up the limit
--- pipelined_requests eval
["GET /hello", "GET /hello", "GET /hello", "GET /hello"]
--- error_code eval
[200, 200, 200, 503]
--- no_error_log
[error]



=== TEST 8: invalid route: missing key
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -156,7 +218,7 @@ GET /t



=== TEST 7: invalid route: wrong count
=== TEST 9: invalid route: wrong count
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -198,7 +260,7 @@ GET /t



=== TEST 8: invalid route: wrong count + POST method
=== TEST 10: invalid route: wrong count + POST method
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -240,7 +302,7 @@ GET /t



=== TEST 9: invalid service: missing key
=== TEST 11: invalid service: missing key
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -280,7 +342,7 @@ GET /t



=== TEST 10: invalid service: wrong count
=== TEST 12: invalid service: wrong count
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -321,7 +383,7 @@ GET /t



=== TEST 11: invalid service: wrong count + POST method
=== TEST 13: invalid service: wrong count + POST method
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -362,7 +424,7 @@ GET /t



=== TEST 12: set route without id in post body
=== TEST 14: set route without id in post body
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -403,7 +465,7 @@ passed



=== TEST 13: up the limit
=== TEST 15: up the limit
--- pipelined_requests eval
["GET /hello", "GET /hello", "GET /hello", "GET /hello"]
--- error_code eval
Expand All @@ -413,7 +475,7 @@ passed



=== TEST 14: disable plugin
=== TEST 16: disable plugin
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -448,7 +510,7 @@ passed



=== TEST 15: up the limit
=== TEST 17: up the limit
--- pipelined_requests eval
["GET /hello", "GET /hello", "GET /hello", "GET /hello"]
--- error_code eval
Expand Down
10 changes: 10 additions & 0 deletions t/plugin/limit-req.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
BEGIN {
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
$SkipReason = "unavailable for the hup tests";

} else {
$ENV{TEST_NGINX_USE_HUP} = 1;
undef $ENV{TEST_NGINX_USE_STAP};
}
}

use t::APISix 'no_plan';

repeat_each(1);
Expand Down

0 comments on commit a626bde

Please sign in to comment.