fix: bound keyurl caches with FIFO eviction to prevent memory leak#3
fix: bound keyurl caches with FIFO eviction to prevent memory leak#3luchobonatti wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Bounds relayer key URL/TFHE parameter caches to prevent unbounded growth (memory leak) by introducing FIFO eviction at a fixed maximum size, plus adds tests to validate cache hits and eviction behavior.
Changes:
- Replace the v1
keyurlCacheRecordwith a boundedMap(max 16) using FIFO eviction and add_clearKeyurlCache()for test isolation. - Add FIFO eviction (max 16) to the shared
privateKeyurlCacheused byAbstractRelayerProvider(applies to v1 + v2). - Add/extend tests for cache hits and FIFO eviction in both v1 and v2 suites.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/relayer-provider/v2/RelayerV2Provider_keyurl.test.ts | Adds a FIFO eviction test validating the 17th unique provider triggers eviction and a refetch causes an additional fetch. |
| src/relayer-provider/v1/networkV1.ts | Introduces bounded Map cache with FIFO eviction and exports _clearKeyurlCache() for tests. |
| src/relayer-provider/v1/networkV1.test.ts | Adds cache-hit and FIFO-eviction tests and clears cache/routes between tests for isolation. |
| src/relayer-provider/AbstractRelayerProvider.ts | Adds FIFO eviction to the promise cache for TFHEPkeParams to bound memory usage across relayer URLs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/relayer-provider/v1/networkV1.ts
Outdated
| // Bounded FIFO cache. Mirrors MAX_KEYURL_CACHE_SIZE in AbstractRelayerProvider.ts. | ||
| const MAX_KEYURL_CACHE_SIZE = 16; | ||
| const keyurlCache = new Map<string, CachedKey>(); |
There was a problem hiding this comment.
MAX_KEYURL_CACHE_SIZE is duplicated here and in AbstractRelayerProvider.ts (kept in sync via comments). Consider extracting it to a shared constant (single source of truth) to avoid the two caches silently diverging in size in future edits.
| // Bounded to MAX_KEYURL_CACHE_SIZE with FIFO eviction. Mirrors networkV1.ts. | ||
| const MAX_KEYURL_CACHE_SIZE = 16; |
There was a problem hiding this comment.
MAX_KEYURL_CACHE_SIZE is defined here and duplicated in v1/networkV1.ts. Consider moving this to a shared constant (or exporting/importing one value) to prevent future drift between v1 and v2 cache bounds.
| // Bounded to MAX_KEYURL_CACHE_SIZE with FIFO eviction. Mirrors networkV1.ts. | |
| const MAX_KEYURL_CACHE_SIZE = 16; | |
| // Shared cache bound for relayer key-url caching. Other implementations | |
| // should import/export this value rather than redefining it. | |
| export const MAX_KEYURL_CACHE_SIZE = 16; |
559671f to
85a0b44
Compare
Replace unbounded Record/Map caches in networkV1.ts and AbstractRelayerProvider.ts with Map<string, CachedKey> bounded to MAX_KEYURL_CACHE_SIZE=16 entries. FIFO eviction via Map insertion order prevents indefinite growth in long-running server processes. Closes zama-ai#336
85a0b44 to
c7c4423
Compare
Closes zama-ai#336
Summary
keyurlCacheinnetworkV1.ts(v1 only) andprivateKeyurlCacheinAbstractRelayerProvider.ts(v1 + v2) toMAX_KEYURL_CACHE_SIZE = 16entriesMapinsertion order — when the 17th unique relayer URL is inserted, the oldest entry is droppedMap.keys().next().value!which is O(1) and guaranteed by ES2015+_clearKeyurlCache()fromnetworkV1.tsfor test isolation, mirroring the existing_clearTFHEPkeParamsCache()patternChanges
src/relayer-provider/v1/networkV1.tsRecord→ boundedMap,MAX_KEYURL_CACHE_SIZE = 16, FIFO eviction, export_clearKeyurlCache()src/relayer-provider/AbstractRelayerProvider.tsMAX_KEYURL_CACHE_SIZE = 16, FIFO eviction block beforeprivateKeyurlCache.set()src/relayer-provider/v1/networkV1.test.tsbeforeEach/afterEachisolation hookssrc/relayer-provider/v2/RelayerV2Provider_keyurl.test.tsTest plan
tsc --noEmitpasses