refactor: replace filteredServerCache with generic syncutil.TTLCache#7277
Conversation
Add TTLCache[K, V] to internal/syncutil with TTL eviction and LRU-cap, then replace the hand-rolled filteredServerCache in server/routed.go with the new type. Move truncateCacheKeyForLog to session_util.go and migrate cache-specific tests to syncutil/ttl_cache_test.go.
There was a problem hiding this comment.
Pull request overview
This PR refactors routed-mode filtered server caching by removing the bespoke filteredServerCache from internal/server/routed.go and replacing it with a new reusable generic syncutil.TTLCache that provides lazy TTL eviction plus an LRU size cap. It also relocates truncateCacheKeyForLog into internal/server/session_util.go and migrates cache behavior tests into the new syncutil test suite.
Changes:
- Added
internal/syncutil/ttl_cache.go: a genericTTLCache[K,V]with TTL + LRU eviction onGetOrCreate, plus injectable clock for deterministic tests. - Updated
internal/server/routed.goto usesyncutil.NewTTLCache[string, *sdk.Server]instead of the removed hand-rolled cache. - Added
internal/syncutil/ttl_cache_test.goand removed the routed-mode cache tests that are now covered in syncutil.
Show a summary per file
| File | Description |
|---|---|
| internal/syncutil/ttl_cache.go | Introduces the new generic TTL+LRU cache implementation used by routed mode. |
| internal/syncutil/ttl_cache_test.go | Adds unit tests for TTLCache behavior (TTL eviction, LRU eviction, concurrency, edge cases). |
| internal/server/session_util.go | Moves truncateCacheKeyForLog alongside other session/log-safety helpers. |
| internal/server/routed.go | Replaces the removed filteredServerCache with syncutil.TTLCache for per-(backend,session) server reuse. |
| internal/server/routed_test.go | Removes cache tests that are now covered by ttl_cache_test.go. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 5/5 changed files
- Comments generated: 3
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
…uncateCacheKeyForLog to session.go
Merged |
internal/server/routed.gocontained a ~70-line hand-rolledfilteredServerCachethat duplicated the get-or-create pattern already present ininternal/syncutil/cache.go, while adding TTL eviction and LRU-cap that the generic helper lacked.Changes
internal/syncutil/ttl_cache.go— New genericTTLCache[K comparable, V any]with lazy TTL eviction and LRU-cap eviction on everyGetOrCreatecall. Includes an injectable clock (newTTLCacheWithClock) for deterministic testing.internal/server/routed.go— DropsfilteredServerCache,filteredServerEntry,newFilteredServerCache, and thegetOrCreatemethod. Replaced withsyncutil.NewTTLCache[string, *sdk.Server]:internal/server/session_util.go—truncateCacheKeyForLogmoved here alongsidetruncateSessionID, its natural home.internal/syncutil/ttl_cache_test.go— 9 tests covering cache hit/miss, TTL eviction (fake clock + real-time), LRU ordering, last-used refresh on hit, concurrent access, andmaxSize=1edge case.internal/server/routed_test.go—TestFilteredServerCache_MaxSizeandTestFilteredServerCache_TTLEvictionremoved; equivalent coverage migrated tosyncutil/ttl_cache_test.go.