You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by we...@apache.org on 2020/09/06 13:52:19 UTC

[apisix] branch master updated: bugfix: updated the value of remaining when `count` changes in limit-count plugin. (#2121)

This is an automated email from the ASF dual-hosted git repository.

wenming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new a8621fb  bugfix: updated the value of remaining when `count` changes in limit-count plugin. (#2121)
a8621fb is described below

commit a8621fb5a13828591bc0c8cbddc8a2f7d630a6db
Author: gy <gu...@163.com>
AuthorDate: Sun Sep 6 21:52:13 2020 +0800

    bugfix: updated the value of remaining when `count` changes in limit-count plugin. (#2121)
---
 apisix/init.lua        |  18 +----
 apisix/plugin.lua      |   4 +-
 t/plugin/limit-count.t | 197 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 204 insertions(+), 15 deletions(-)

diff --git a/apisix/init.lua b/apisix/init.lua
index 6e20cfd..63275c1 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -398,21 +398,11 @@ function _M.http_access_phase()
             return core.response.exit(404)
         end
 
-        local changed
-        route, changed = plugin.merge_service_route(service, route)
+        route = plugin.merge_service_route(service, route)
         api_ctx.matched_route = route
-
-        if changed then
-            api_ctx.conf_type = "route&service"
-            api_ctx.conf_version = route.modifiedIndex .. "&"
-                                   .. service.modifiedIndex
-            api_ctx.conf_id = route.value.id .. "&"
-                              .. service.value.id
-        else
-            api_ctx.conf_type = "service"
-            api_ctx.conf_version = service.modifiedIndex
-            api_ctx.conf_id = service.value.id
-        end
+        api_ctx.conf_type = "route&service"
+        api_ctx.conf_version = route.modifiedIndex .. "&" .. service.modifiedIndex
+        api_ctx.conf_id = route.value.id .. "&" .. service.value.id
     else
         api_ctx.conf_type = "route"
         api_ctx.conf_version = route.modifiedIndex
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 9a07a50..0d003be 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -297,6 +297,7 @@ local function merge_service_route(service_conf, route_conf)
     local new_conf = core.table.deepcopy(service_conf)
     new_conf.value.service_id = new_conf.value.id
     new_conf.value.id = route_conf.value.id
+    new_conf.modifiedIndex = route_conf.modifiedIndex
 
     if route_conf.value.plugins then
         for name, conf in pairs(route_conf.value.plugins) do
@@ -332,7 +333,8 @@ function _M.merge_service_route(service_conf, route_conf)
     core.log.info("service conf: ", core.json.delay_encode(service_conf))
     core.log.info("  route conf: ", core.json.delay_encode(route_conf))
 
-    return merged_route(route_conf, service_conf,
+    local route_service_key = route_conf.modifiedIndex .. "#" .. service_conf.modifiedIndex
+    return merged_route(route_service_key, service_conf,
                         merge_service_route,
                         service_conf, route_conf)
 end
diff --git a/t/plugin/limit-count.t b/t/plugin/limit-count.t
index 0e22937..d04e22e 100644
--- a/t/plugin/limit-count.t
+++ b/t/plugin/limit-count.t
@@ -637,3 +637,200 @@ GET /t
 passed
 --- no_error_log
 [error]
+
+
+
+=== TEST 21: when the count is changed, check  the limit is correct
+--- config
+    location /t1 {
+        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": 1,
+                                "time_window": 60,
+                                "key": "remote_addr"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "plugins": {
+                                "limit-count": {
+                                    "rejected_code": 503
+                                }
+                            }
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }    
+--- pipelined_requests eval
+["GET /hello", "GET /hello","GET /hello","GET /t1", "GET /hello","GET /hello"]
+--- error_code eval
+[200, 200, 503, 200, 200, 503]
+--- no_error_log
+[error]
+
+
+
+=== TEST 22: when the count is changed, check  the limit is correct(from 1 to 2)
+--- 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": 2,
+                                "time_window": 60,
+                                "key": "remote_addr"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "plugins": {
+                                "limit-count": {
+                                    "rejected_code": 503
+                                }
+                            }
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+    location /t1 {
+        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": 1,
+                                "time_window": 60,
+                                "key": "remote_addr"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "plugins": {
+                                "limit-count": {
+                                    "rejected_code": 503
+                                }
+                            }
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- pipelined_requests eval
+["GET /t1", "GET /hello", "GET /hello", "GET /t", "GET /hello", "GET /hello", "GET /hello"]
+--- error_code eval
+[200, 200, 503, 200, 200, 200, 503]
+--- no_error_log
+[error]
+
+
+
+=== TEST 23: when the count is changed, check  the limit is correct(from 2 to 2)
+--- 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": 2,
+                                "time_window": 60,
+                                "key": "remote_addr"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "plugins": {
+                                "limit-count": {
+                                    "rejected_code": 503
+                                }
+                            }
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- pipelined_requests eval
+["GET /t", "GET /hello", "GET /hello", "GET /hello", "GET /t", "GET /hello", "GET /hello", "GET /hello"]
+--- error_code eval
+[200, 200, 200, 503, 200, 200, 200, 503]
+--- no_error_log
+[error]