Repository navigation
Conversation
The cache only ever grew: expired entries were skipped on lookup but never removed, and nothing capped the number of keys, so a long-lived process touching many URIs or list() prefixes held every response for its life. Prune expired entries on store (covering both get() and list() keys) and keep the cache in an OrderedDict as a bounded LRU (cache_max_entries, default 1024). Cache operations take a lock because lookups are now read-modify-write and subscribe()'s handler touches the cache from its own thread. Signed-off-by: bfs2021 <101915083+bfs2021@users.noreply.github.com>
Member
|
Hi @bfs2021, thank you for taking the time to send this. We're closing it, and it's not about the quality of the change. The issue it fixes is labelled If you'd like to keep contributing, we'd welcome help on our Thanks again! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
cache_ttlset, entries inPromptClient._cacheare only skipped once expired, never removed, and the cache has no size limit. A long-lived process that touches many URIs orlist()prefixes (for example one URI per customer) therefore retains every response it has ever fetched. Fixes #26.Root cause
get()andlist()store(value, expiry)in a plain dict keyed by the URI /("list", prefix). On a lookup after expiry the code falls through and overwrites the same key, and the only deletion anywhere is thepopfor an accepted subscription reload insubscribe(). Distinct keys that are never queried again have no removal path at all.Fix
OrderedDictand sweep expired entries whenever a new entry is stored, covering bothget()andlist()key shapes. Removing only on lookup was not enough: the reproduction inserts 10,000 distinct URIs, so no key is ever looked up twice.cache_max_entries(default 1024) and evict the least recently used entry when the bound is exceeded; a hit moves the key to the end.threading.Lock: lookups are now read-modify-write (delete expired +move_to_end), andsubscribe()'s handler pops the cache from its own thread.Testing
pytest -q: 30 passed.test_expired_entries_are_removed_for_get_and_list(fails onmain: 101 entries retained, passes with the sweep) andtest_cache_is_bounded_and_evicts_least_recently_used(LRU order plus a 10-insert run staying at the bound).