Skip to content

Commit

Permalink
perf(*): reduce subschema memory usage
Browse files Browse the repository at this point in the history
Signed-off-by: Aapo Talvensaari <[email protected]>
  • Loading branch information
bungle committed Aug 30, 2024
1 parent d10408c commit 02f078f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 27 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/reduce-subschema-memory-usage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Reduce sub-schema memory usage on start by 10% when using bundled plugins.
type: performance
scope: Core
44 changes: 29 additions & 15 deletions kong/db/schema/plugin_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,44 @@ local fmt = string.format
local tostring = tostring


local SUBSCHEMA_CACHE = setmetatable({}, { __mode = "kv" })


local plugin_loader = {}


function plugin_loader.load_subschema(parent_schema, plugin, errors)
local plugin_schema = "kong.plugins." .. plugin .. ".schema"
local ok, schema = load_module_if_exists(plugin_schema)
if not ok then
ok, schema = plugin_servers.load_schema(plugin)
end
local schema = SUBSCHEMA_CACHE[plugin]
if not schema then
local ok
ok, schema = load_module_if_exists("kong.plugins." .. plugin .. ".schema")
if not ok then
ok, schema = plugin_servers.load_schema(plugin)
end

if not ok then
return nil, "no configuration schema found for plugin: " .. plugin
if not ok then
return nil, "no configuration schema found for plugin: " .. plugin
end

local err_t
ok, err_t = MetaSchema.MetaSubSchema:validate(schema)
if not ok then
return nil, tostring(errors:schema_violation(err_t))
end

SUBSCHEMA_CACHE[plugin] = schema
end

local err_t
ok, err_t = MetaSchema.MetaSubSchema:validate(schema)
if not ok then
return nil, tostring(errors:schema_violation(err_t))
if not SUBSCHEMA_CACHE[parent_schema] then
SUBSCHEMA_CACHE[parent_schema] = {}
end

local err
ok, err = Entity.new_subschema(parent_schema, plugin, schema)
if not ok then
return nil, "error initializing schema for plugin: " .. err
if not SUBSCHEMA_CACHE[parent_schema][plugin] then
SUBSCHEMA_CACHE[parent_schema][plugin] = true
local ok, err = Entity.new_subschema(parent_schema, plugin, schema)
if not ok then
return nil, "error initializing schema for plugin: " .. err
end
end

return schema
Expand Down
38 changes: 26 additions & 12 deletions kong/db/schema/vault_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@ local load_module_if_exists = require "kong.tools.module".load_module_if_exists
local tostring = tostring


local SUBSCHEMA_CACHE = setmetatable({}, { __mode = "kv" })


local vault_loader = {}


function vault_loader.load_subschema(parent_schema, vault, errors)
local vault_schema = "kong.vaults." .. vault .. ".schema"
local ok, schema = load_module_if_exists(vault_schema)
if not ok then
return nil, "no configuration schema found for vault: " .. vault
local schema = SUBSCHEMA_CACHE[vault]
if not schema then
local ok
ok, schema = load_module_if_exists("kong.vaults." .. vault .. ".schema")
if not ok then
return nil, "no configuration schema found for vault: " .. vault
end

local err_t
ok, err_t = MetaSchema.MetaSubSchema:validate(schema)
if not ok then
return nil, tostring(errors:schema_violation(err_t))
end

SUBSCHEMA_CACHE[vault] = schema
end

local err_t
ok, err_t = MetaSchema.MetaSubSchema:validate(schema)
if not ok then
return nil, tostring(errors:schema_violation(err_t))
if not SUBSCHEMA_CACHE[parent_schema] then
SUBSCHEMA_CACHE[parent_schema] = {}
end

local err
ok, err = Entity.new_subschema(parent_schema, vault, schema)
if not ok then
return nil, "error initializing schema for vault: " .. err
if not SUBSCHEMA_CACHE[parent_schema][vault] then
SUBSCHEMA_CACHE[parent_schema][vault] = true
local ok, err = Entity.new_subschema(parent_schema, vault, schema)
if not ok then
return nil, "error initializing schema for vault: " .. err
end
end

return schema
Expand Down

0 comments on commit 02f078f

Please sign in to comment.