nic-6443 commented on code in PR #14061:
URL: https://github.com/apache/apisix/pull/14061#discussion_r4355788932
##########
apisix/plugins/oas-validator.lua:
##########
@@ -176,6 +184,45 @@ local function fetch_and_compile(conf)
return validator
end
+local function get_fresh_shared_entry(cache_key, ttl)
+ local entry = shared_spec_url_entries[cache_key]
+ if entry and ngx_now() - entry.fetched_at < ttl then
+ return entry
+ end
+end
+
+local function create_spec_url_entry(conf, cache_key, ttl)
+ local entry = get_fresh_shared_entry(cache_key, ttl)
+ if entry then
+ return entry
+ end
+
+ -- spec_url_lrucache only serialises per resource, so resources sharing a
+ -- cold URL would otherwise each fetch and keep their own compiled copy
+ local lock, err = resty_lock:new("lrucache-lock")
+ if not lock then
+ return nil, "failed to create lock: " .. err
+ end
+
+ local elapsed, lock_err = lock:lock(plugin_name .. "#" .. cache_key)
+ if not elapsed then
+ return nil, "failed to acquire the lock: " .. lock_err
+ end
+
+ entry = get_fresh_shared_entry(cache_key, ttl)
+ if not entry then
+ local validator
+ validator, err = fetch_and_compile(conf)
+ if validator then
+ entry = {validator = validator, fetched_at = ngx_now()}
+ shared_spec_url_entries[cache_key] = entry
Review Comment:
You're right, and this was a design problem rather than a missing step: as
long as each resource holds its own reference, an idle resource pins whatever
generation it last saw. I've dropped the resource-keyed slots and the
weak-valued table entirely in 5d1d17e. The cache is back to master's single
URL-keyed entry, so a refresh replaces the only reference there is and the old
generation is collectable right away, no matter how many resources share the
URL.
The leak itself is now handled in `core.lrucache` with a new `inactive`
option (oas-validator passes `inactive = spec_url_ttl`): a background purge
deletes entries not accessed within `inactive`. Your scenario is TEST 27 in
`oas-validator3.t`: one route refreshing and three idle routes leave one
compiled spec after full GC, and zero once nobody has used the URL for
`spec_url_ttl`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]