Skip to content

Commit 5f7dbc1

Browse files
committed
perf: cache policy manifests
Previously, every time the policy chain was rebuilt, the gateway would look for the manifests file on the disk. This caused a delay for incoming requests. Since the manifests are static and do not change at runtime, it is more efficient to cache them at the module level. This caching will speed up the lookup process.
1 parent 4fa6cfd commit 5f7dbc1

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

gateway/src/apicast/policy_loader.lua

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ local concat = table.concat
1616
local setmetatable = setmetatable
1717
local pcall = pcall
1818

19+
local tab_new = require('table.new')
20+
local isempty = require('table.isempty')
21+
22+
local manifests_cache = tab_new(32, 0)
23+
1924
local _M = {}
2025

2126
local resty_env = require('resty.env')
@@ -70,8 +75,22 @@ local function lua_load_path(load_path)
7075
return format('%s/?.lua', load_path)
7176
end
7277

78+
local function get_manifest(name, version)
79+
local manifests = manifests_cache[name]
80+
if manifests then
81+
for _, manifest in ipairs(manifests) do
82+
if version == manifest.version then
83+
return manifest
84+
end
85+
end
86+
end
87+
end
88+
7389
local function load_manifest(name, version, path)
74-
local manifest = read_manifest(path)
90+
local manifest = get_manifest(name, version)
91+
if not manifest then
92+
manifest = read_manifest(path)
93+
end
7594

7695
if manifest then
7796
if manifest.version ~= version then
@@ -110,8 +129,8 @@ end
110129
function _M:load_path(name, version, paths)
111130
local failures = {}
112131

113-
for _, path in ipairs(paths or self.policy_load_paths()) do
114-
local manifest, load_path = load_manifest(name, version, format('%s/%s/%s', path, name, version) )
132+
if version == 'builtin' then
133+
local manifest, load_path = load_manifest(name, version, format('%s/%s', self.builtin_policy_load_path(), name) )
115134

116135
if manifest then
117136
return load_path, manifest.configuration
@@ -120,8 +139,8 @@ function _M:load_path(name, version, paths)
120139
end
121140
end
122141

123-
if version == 'builtin' then
124-
local manifest, load_path = load_manifest(name, version, format('%s/%s', self.builtin_policy_load_path(), name) )
142+
for _, path in ipairs(paths or self.policy_load_paths()) do
143+
local manifest, load_path = load_manifest(name, version, format('%s/%s/%s', path, name, version) )
125144

126145
if manifest then
127146
return load_path, manifest.configuration
@@ -130,6 +149,7 @@ function _M:load_path(name, version, paths)
130149
end
131150
end
132151

152+
133153
return nil, nil, failures
134154
end
135155

@@ -173,9 +193,15 @@ end
173193
-- Returns all the policy modules
174194
function _M:get_all()
175195
local policy_modules = {}
196+
local manifests
176197

177-
local policy_manifests_loader = require('apicast.policy_manifests_loader')
178-
local manifests = policy_manifests_loader.get_all()
198+
if isempty(manifests_cache) then
199+
local policy_manifests_loader = require('apicast.policy_manifests_loader')
200+
manifests = policy_manifests_loader.get_all()
201+
manifests_cache = manifests
202+
else
203+
manifests = manifests_cache
204+
end
179205

180206
for policy_name, policy_manifests in pairs(manifests) do
181207
for _, manifest in ipairs(policy_manifests) do

0 commit comments

Comments
 (0)