Skip to content

Commit fd4d94a

Browse files
bunglelocao
authored andcommitted
fix(declarative) lazy load full schema for validation and store it in partial schema
### Summary It was reported on Kong#6547 that each time a new configuration is loaded to Kong the memory is leaked. On the issue thread there are information of the where the leak starts and where the actual leak happens. See: Kong#6547 (comment) and: Kong#6547 (comment) This PR changes the code so that we only (lazy) load the `full schema` that is needed for `full validation` only once and then we store that into partial schema. This means that we are not constantly re-creating the fullschema that seems to be cached differently inside `schema.lua` on each invocation as it uses tables as cache keys and the load seem to make new tables for fields each time we call `Declarative.load`. I am not 100% sure does it remove the leaks fully but here is a test that I did manually: #### Numbers without Patch Worker memory usage when started with example `kong.yaml`: 110 MB Worker memory usage after making 100 config updates to `:8001/config`: 714 MB #### Numbers with Patch (this commit) 110 MB (when started) 237 MB (after 100 updates) ### Issues Resolved Fix Kong#6547
1 parent 228e8f1 commit fd4d94a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

kong/db/schema/others/declarative_config.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,13 @@ local function flatten(self, input)
621621
-- the error may be due entity validation that depends on foreign entity,
622622
-- and that is the reason why we try to validate the input again with the
623623
-- filled foreign keys
624+
if not self.full_schema then
625+
self.full_schema = DeclarativeConfig.load(self.plugin_set, true)
626+
end
627+
624628
local input_copy = utils.deep_copy(input, false)
625629
populate_ids_for_validation(input_copy, self.known_entities)
626-
local schema = DeclarativeConfig.load(self.plugin_set, true)
627-
628-
local ok2, err2 = schema:validate(input_copy)
630+
local ok2, err2 = self.full_schema:validate(input_copy)
629631
if not ok2 then
630632
local err3 = utils.deep_merge(err2, extract_null_errors(err))
631633
return nil, err3

0 commit comments

Comments
 (0)