fix(store): bound MemPublicKeyStore cache to prevent memory exhaustion#93
Open
emrul wants to merge 1 commit into
Open
fix(store): bound MemPublicKeyStore cache to prevent memory exhaustion#93emrul wants to merge 1 commit into
emrul wants to merge 1 commit into
Conversation
Member
|
I think this should use an LRU cache instead of blindly clearing the map once the size limit is reached. |
The in-memory public key cache was unbounded — every distinct 32-byte public key passed through `public_key()` added an entry that persisted for the lifetime of the store. The existing source already carried a `TODO: Make max number of keys stored configurable.` comment acknowledging this. This matters because the cache is fed from sync-peer input via the `insert_remote_entry → validate_entry → SignedEntry::verify → public_key` call chain. A peer that sends entries signed by many distinct authors grows the victim's map one entry per distinct author forever. Fix: cap the cache at `MAX_CACHED_KEYS = 10_000` using an `lru::LruCache`. When the cache is full, the least-recently-used entry is evicted. The `lru` crate is already transitively present in the dependency tree via `iroh-relay`, so this adds no new compile.
6bb86ff to
73bab94
Compare
Author
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.
Description
The in-memory public-key cache on
MemPublicKeyStorewas unbounded — every distinct 32-byte public key passed throughpublic_key()added a cache entry that persisted for the lifetime of the store. The existing source already carried aTODO: Make max number of keys stored configurable.comment acknowledging this. This PR addresses that low-hanging fruit.The cache is fed from sync-peer input via
insert_remote_entry → validate_entry → SignedEntry::verify → PublicKeyStore::public_key. A peer that sends entries signed by many distinct authors grows the victim's map one entry per distinct 32-byte author/namespace key, forever.This PR caps the cache at
MAX_CACHED_KEYS = 10_000usinglru::LruCache. Thelrucrate is already transitively present viairoh-relay, so chose to add this as a direct dependency to avoid adding something new to the project.Two tests cover the change:
cache_is_bounded_under_unique_key_floodfeeds 15,000 distinct valid ed25519 keys through the cache and asserts the size stays ≤MAX_CACHED_KEYS;recently_used_keys_survive_evictionfills the cache to capacity, touches the first key, inserts 100 more distinct keys, and asserts the touched key is still cached while the oldest untouched one has been evicted.Breaking Changes
None at the API surface.
MemPublicKeyStore's internal representation changed fromHashMaptoLruCache; the publicPublicKeyStoretrait is unchanged. Behaviour change: once the cache reaches 10,000 entries, previously-cached keys may be evicted on subsequent misses (re-verification cost is a singleVerifyingKey::from_bytescall).Notes & open questions
Change checklist