From f55614cc59b4d921306b8234cb7a6ad3163d27d0 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 9 Aug 2023 17:58:36 +0800 Subject: [PATCH] Revert "style(router): remove `ipairs` and optimize router select (#11273)" This reverts commit 073d2fd54c8049f87a60680a2ee4c3a7ca88b21a. --- kong/router/atc.lua | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/kong/router/atc.lua b/kong/router/atc.lua index fc8bd6a64f4a..0901e5960a45 100644 --- a/kong/router/atc.lua +++ b/kong/router/atc.lua @@ -17,6 +17,7 @@ local type = type local assert = assert local setmetatable = setmetatable local pairs = pairs +local ipairs = ipairs local tonumber = tonumber @@ -77,8 +78,8 @@ do CACHED_SCHEMA = schema.new() for typ, fields in pairs(FIELDS) do - for i = 1, #fields do - assert(CACHED_SCHEMA:add_field(fields[i], typ)) + for _, v in ipairs(fields) do + assert(CACHED_SCHEMA:add_field(v, typ)) end end @@ -165,8 +166,8 @@ end local function has_header_matching_field(fields) - for i = 1, #fields do - if is_http_headers_field(fields[i]) then + for _, field in ipairs(fields) do + if is_http_headers_field(field) then return true end end @@ -224,7 +225,6 @@ local function new_from_scratch(routes, get_exp_and_priority) routes = routes_t, services = services_t, fields = fields, - fields_n = #fields, match_headers = match_headers, updated_at = new_updated_at, rebuilding = false, @@ -312,7 +312,6 @@ local function new_from_previous(routes, get_exp_and_priority, old_router) local fields = inst:get_fields() old_router.fields = fields - old_router.fields_n = #fields old_router.match_headers = has_header_matching_field(fields) old_router.updated_at = new_updated_at old_router.rebuilding = false @@ -403,9 +402,7 @@ function _M:select(req_method, req_uri, req_host, req_scheme, local host, port = split_host_port(req_host) - for i = 1, self.fields_n do - local field = self.fields[i] - + for _, field in ipairs(self.fields) do if field == "http.method" then assert(c:add_value(field, req_method)) @@ -445,8 +442,8 @@ function _M:select(req_method, req_uri, req_host, req_scheme, end else - for idx = 1, #v do - local res, err = c:add_value(field, v[idx]:lower()) + for _, v in ipairs(v) do + local res, err = c:add_value(field, v:lower()) if not res then return nil, err end @@ -512,8 +509,8 @@ do local name = name:gsub("-", "_"):lower() if type(value) == "table" then - for i = 1, #value do - value[i] = value[i]:lower() + for i, v in ipairs(value) do + value[i] = v:lower() end tb_sort(value) value = tb_concat(value, ", ") @@ -613,9 +610,7 @@ function _M:select(_, _, _, scheme, local c = context.new(self.schema) - for i = 1, self.fields_n do - local field = self.fields[i] - + for _, field in ipairs(self.fields) do if field == "net.protocol" then assert(c:add_value(field, scheme))