nic-6443 commented on code in PR #12800:
URL: https://github.com/apache/apisix/pull/12800#discussion_r2601218177


##########
apisix/plugin.lua:
##########
@@ -1288,37 +1288,52 @@ function _M.run_global_rules(api_ctx, global_rules, 
phase_name)
             end
         end
 
-        local plugins = core.tablepool.fetch("plugins", 32, 0)
-        local route = api_ctx.matched_route
+        local all_plugins = {}
+        local createdIndex = 1
+        local modifiedIndex = 1
+        -- merge all plugins across all global rules to one dummy global_rule 
structure
         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
-
-            core.table.clear(plugins)
-            plugins = _M.filter(api_ctx, global_rule, plugins, route)
-
-            -- Remove duplicate plugins from the plugins table
-            local i = 1
-            while i <= #plugins do
-                local plugin = plugins[i]
-                local plugin_name = plugin.name
-                if duplicate_plugins[plugin_name] then
-                    -- Remove duplicate plugin and its config
-                    core.table.remove(plugins, i)
-                    core.table.remove(plugins, i)
-                else
-                    -- Move to next plugin pair
-                    i = i + 2
+            if global_rule.value and global_rule.value.plugins then
+                core.table.merge(all_plugins, global_rule.value.plugins)
+                if global_rule.modifiedIndex > modifiedIndex then
+                    modifiedIndex = global_rule.modifiedIndex
+                    createdIndex = global_rule.createdIndex
                 end
             end
+        end
 
-            if phase_name == nil then
-                _M.run_plugin("rewrite", plugins, api_ctx)
-                _M.run_plugin("access", plugins, api_ctx)
-            else
-                _M.run_plugin(phase_name, plugins, api_ctx)
-            end
+        -- remove duplicate plugins
+        for plugin_name, _ in pairs(duplicate_plugins) do
+            all_plugins[plugin_name] = nil
+        end
+
+        local dummy_global_rule = {
+            key = "/apisix/global_rules/1",
+            value = {
+                updated_time = ngx.time(),
+                plugins = all_plugins,
+                created_time = ngx.time(),
+                id = 1,
+            },
+            createdIndex = createdIndex,
+            modifiedIndex = modifiedIndex,
+            clean_handlers = {},
+        }

Review Comment:
   I think we need to cache the result of this merge. We can use 
`global_rules.conf_version` as the cache key and the final `modifiedIndex` to 
avoid executing the merge code repeatedly for each request.
   
https://github.com/apache/apisix/blob/896d3c389196e7a06fddeb5c03006bf300fa4704/apisix/global_rules.lua#L45



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to