@@ -59,6 +59,10 @@ local expr_lrucache = core.lrucache.new({
5959local meta_pre_func_load_lrucache = core .lrucache .new ({
6060 ttl = 300 , count = 512
6161})
62+ local merge_global_rule_lrucache = core .lrucache .new ({
63+ ttl = 300 , count = 512
64+ })
65+
6266local local_conf
6367local check_plugin_metadata
6468
@@ -1263,7 +1267,59 @@ function _M.set_plugins_meta_parent(plugins, parent)
12631267end
12641268
12651269
1266- function _M .run_global_rules (api_ctx , global_rules , phase_name )
1270+ local function merge_global_rules (global_rules )
1271+ -- First pass: identify duplicate plugins across all global rules
1272+ local plugin_count = {}
1273+ local duplicate_plugins = {}
1274+ local values = global_rules
1275+ for _ , global_rule in config_util .iterate_values (values ) do
1276+ if global_rule .value and global_rule .value .plugins then
1277+ for plugin_name , _ in pairs (global_rule .value .plugins ) do
1278+ plugin_count [plugin_name ] = (plugin_count [plugin_name ] or 0 ) + 1
1279+ if plugin_count [plugin_name ] > 1 then
1280+ duplicate_plugins [plugin_name ] = true
1281+ end
1282+ end
1283+ end
1284+ end
1285+
1286+ local all_plugins = {}
1287+ local createdIndex = 1
1288+ local modifiedIndex = 1
1289+ -- merge all plugins across all global rules to one dummy global_rule structure
1290+ for _ , global_rule in config_util .iterate_values (values ) do
1291+ if global_rule .value and global_rule .value .plugins then
1292+ core .table .merge (all_plugins , global_rule .value .plugins )
1293+ if global_rule .modifiedIndex > modifiedIndex then
1294+ modifiedIndex = global_rule .modifiedIndex
1295+ createdIndex = global_rule .createdIndex
1296+ end
1297+ end
1298+ end
1299+
1300+ -- remove duplicate plugins
1301+ for plugin_name , _ in pairs (duplicate_plugins ) do
1302+ all_plugins [plugin_name ] = nil
1303+ end
1304+
1305+ local dummy_global_rule = {
1306+ key = " /apisix/global_rules/1" ,
1307+ value = {
1308+ updated_time = ngx .time (),
1309+ plugins = all_plugins ,
1310+ created_time = ngx .time (),
1311+ id = 1 ,
1312+ },
1313+ createdIndex = createdIndex ,
1314+ modifiedIndex = modifiedIndex ,
1315+ clean_handlers = {},
1316+ }
1317+
1318+ return dummy_global_rule
1319+ end
1320+
1321+
1322+ function _M .run_global_rules (api_ctx , global_rules , conf_version , phase_name )
12671323 if global_rules and # global_rules > 0 then
12681324 local orig_conf_type = api_ctx .conf_type
12691325 local orig_conf_version = api_ctx .conf_version
@@ -1273,53 +1329,12 @@ function _M.run_global_rules(api_ctx, global_rules, phase_name)
12731329 api_ctx .global_rules = global_rules
12741330 end
12751331
1276- -- First pass: identify duplicate plugins across all global rules
1277- local plugin_count = {}
1278- local duplicate_plugins = {}
1279- local values = global_rules
1280- for _ , global_rule in config_util .iterate_values (values ) do
1281- if global_rule .value and global_rule .value .plugins then
1282- for plugin_name , _ in pairs (global_rule .value .plugins ) do
1283- plugin_count [plugin_name ] = (plugin_count [plugin_name ] or 0 ) + 1
1284- if plugin_count [plugin_name ] > 1 then
1285- duplicate_plugins [plugin_name ] = true
1286- end
1287- end
1288- end
1289- end
1290-
1291- local all_plugins = {}
1292- local createdIndex = 1
1293- local modifiedIndex = 1
1294- -- merge all plugins across all global rules to one dummy global_rule structure
1295- for _ , global_rule in config_util .iterate_values (values ) do
1296- if global_rule .value and global_rule .value .plugins then
1297- core .table .merge (all_plugins , global_rule .value .plugins )
1298- if global_rule .modifiedIndex > modifiedIndex then
1299- modifiedIndex = global_rule .modifiedIndex
1300- createdIndex = global_rule .createdIndex
1301- end
1302- end
1332+ local dummy_global_rule = merge_global_rule_lrucache (conf_version , global_rules , merge_global_rules , global_rules )
1333+ if not dummy_global_rule then
1334+ core .log .error (" failed to get merged global rules form cache, using merge_global_rules instead" )
1335+ dummy_global_rule = merge_global_rules (global_rules )
13031336 end
13041337
1305- -- remove duplicate plugins
1306- for plugin_name , _ in pairs (duplicate_plugins ) do
1307- all_plugins [plugin_name ] = nil
1308- end
1309-
1310- local dummy_global_rule = {
1311- key = " /apisix/global_rules/1" ,
1312- value = {
1313- updated_time = ngx .time (),
1314- plugins = all_plugins ,
1315- created_time = ngx .time (),
1316- id = 1 ,
1317- },
1318- createdIndex = createdIndex ,
1319- modifiedIndex = modifiedIndex ,
1320- clean_handlers = {},
1321- }
1322-
13231338 local plugins = core .tablepool .fetch (" plugins" , 32 , 0 )
13241339 local route = api_ctx .matched_route
13251340 api_ctx .conf_type = " global_rule"
0 commit comments