From 7b6796d319abcb32cf67f97238ffc12a79bf3ee5 Mon Sep 17 00:00:00 2001 From: spacewander Date: Mon, 27 Apr 2020 19:52:43 +0800 Subject: [PATCH 1/3] fix: skip tombstone mark when iterating the global values Close #1473. --- apisix/core/config_util.lua | 43 +++++++++++++++++++++++++++++++++++++ apisix/init.lua | 7 ++++-- t/node/global-rule.t | 7 ++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 apisix/core/config_util.lua diff --git a/apisix/core/config_util.lua b/apisix/core/config_util.lua new file mode 100644 index 000000000000..df731cf82861 --- /dev/null +++ b/apisix/core/config_util.lua @@ -0,0 +1,43 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + + +local _M = {} + + +local function _iterate_values(self, tab) + while true do + self.idx = self.idx + 1 + local v = tab[self.idx] + if type(v) == "table" then + return self.idx, v + end + if v == nil then + return nil, nil + end + -- skip the tombstone + end +end + + +function _M.iterate_values(tab) + local iter = setmetatable({idx = 0}, {__call = _iterate_values}) + return iter, tab, 0 +end + + +return _M diff --git a/apisix/init.lua b/apisix/init.lua index aa8598e87775..e200747ac97c 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -16,6 +16,7 @@ -- local require = require local core = require("apisix.core") +local config_util = require("apisix.core.config_util") local plugin = require("apisix.plugin") local service_fetch = require("apisix.http.service").get local admin_init = require("apisix.admin.init") @@ -263,7 +264,8 @@ function _M.http_access_phase() if router.global_rules and router.global_rules.values and #router.global_rules.values > 0 then local plugins = core.tablepool.fetch("plugins", 32, 0) - for _, global_rule in ipairs(router.global_rules.values) do + local values = router.global_rules.values + for _, global_rule in config_util.iterate_values(values) do api_ctx.conf_type = "global_rule" api_ctx.conf_version = global_rule.modifiedIndex api_ctx.conf_id = global_rule.value.id @@ -451,7 +453,8 @@ local function common_phase(plugin_name) and #router.global_rules.values > 0 then local plugins = core.tablepool.fetch("plugins", 32, 0) - for _, global_rule in ipairs(router.global_rules.values) do + local values = router.global_rules.values + for _, global_rule in config_util.iterate_values(values) do core.table.clear(plugins) plugins = plugin.filter(global_rule, plugins) run_plugin(plugin_name, plugins, api_ctx) diff --git a/t/node/global-rule.t b/t/node/global-rule.t index c1de5dd33c6f..a31a013a23bc 100644 --- a/t/node/global-rule.t +++ b/t/node/global-rule.t @@ -131,11 +131,18 @@ GET /hello ngx.status = code end ngx.say(body) + + local code, body = t('/not_found', ngx.HTTP_GET) + ngx.say(code) + local code, body = t('/not_found', ngx.HTTP_GET) + ngx.say(code) } } --- request GET /t --- response_body passed +404 +404 --- no_error_log [error] From 6352a49e95ce6ccb1962460ecc611d85959f358d Mon Sep 17 00:00:00 2001 From: spacewander Date: Mon, 27 Apr 2020 21:32:52 +0800 Subject: [PATCH 2/3] remove unused ipairs --- apisix/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/init.lua b/apisix/init.lua index e200747ac97c..48c5b8098bdf 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -28,7 +28,6 @@ local get_method = ngx.req.get_method local ngx_exit = ngx.exit local math = math local error = error -local ipairs = ipairs local pairs = pairs local tostring = tostring local load_balancer From f3e58c0772b7dec4135572dc386458ecf2f700fb Mon Sep 17 00:00:00 2001 From: spacewander Date: Mon, 27 Apr 2020 21:44:00 +0800 Subject: [PATCH 3/3] not global variable --- apisix/core/config_util.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apisix/core/config_util.lua b/apisix/core/config_util.lua index df731cf82861..1e399217b6da 100644 --- a/apisix/core/config_util.lua +++ b/apisix/core/config_util.lua @@ -14,6 +14,8 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- +local setmetatable = setmetatable +local type = type local _M = {}