Skip to content

Commit 939986a

Browse files
cache
Signed-off-by: Abhishek Choudhary <[email protected]>
1 parent 1287bab commit 939986a

File tree

2 files changed

+67
-51
lines changed

2 files changed

+67
-51
lines changed

apisix/init.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ local function common_phase(phase_name)
468468
return
469469
end
470470

471-
plugin.run_global_rules(api_ctx, api_ctx.global_rules, phase_name)
471+
local global_rules, conf_version = apisix_global_rules.global_rules()
472+
plugin.run_global_rules(api_ctx, global_rules, conf_version, phase_name)
472473

473474
if api_ctx.script_obj then
474475
script.run(phase_name, api_ctx)
@@ -721,8 +722,8 @@ function _M.http_access_phase()
721722
local route = api_ctx.matched_route
722723
if not route then
723724
-- run global rule when there is no matching route
724-
local global_rules = apisix_global_rules.global_rules()
725-
plugin.run_global_rules(api_ctx, global_rules, nil)
725+
local global_rules, conf_version = apisix_global_rules.global_rules()
726+
plugin.run_global_rules(api_ctx, global_rules, conf_version, nil)
726727

727728
core.log.info("not find any matched route")
728729
return core.response.exit(404,
@@ -774,8 +775,8 @@ function _M.http_access_phase()
774775
api_ctx.route_name = route.value.name
775776

776777
-- run global rule
777-
local global_rules = apisix_global_rules.global_rules()
778-
plugin.run_global_rules(api_ctx, global_rules, nil)
778+
local global_rules, conf_version = apisix_global_rules.global_rules()
779+
plugin.run_global_rules(api_ctx, global_rules, conf_version, nil)
779780

780781
if route.value.script then
781782
script.load(route, api_ctx)

apisix/plugin.lua

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ local expr_lrucache = core.lrucache.new({
5959
local 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+
6266
local local_conf
6367
local check_plugin_metadata
6468

@@ -1263,7 +1267,59 @@ function _M.set_plugins_meta_parent(plugins, parent)
12631267
end
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

Comments
 (0)