Skip to content

refactor: replace filteredServerCache with generic syncutil.TTLCache#7277

Merged
lpcox merged 7 commits into
mainfrom
copilot/aw-abc123def456-fix-duplicate-code
Jun 10, 2026
Merged

refactor: replace filteredServerCache with generic syncutil.TTLCache#7277
lpcox merged 7 commits into
mainfrom
copilot/aw-abc123def456-fix-duplicate-code

Conversation

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

internal/server/routed.go contained a ~70-line hand-rolled filteredServerCache that duplicated the get-or-create pattern already present in internal/syncutil/cache.go, while adding TTL eviction and LRU-cap that the generic helper lacked.

Changes

  • internal/syncutil/ttl_cache.go — New generic TTLCache[K comparable, V any] with lazy TTL eviction and LRU-cap eviction on every GetOrCreate call. Includes an injectable clock (newTTLCacheWithClock) for deterministic testing.

  • internal/server/routed.go — Drops filteredServerCache, filteredServerEntry, newFilteredServerCache, and the getOrCreate method. Replaced with syncutil.NewTTLCache[string, *sdk.Server]:

    // Before: ~70-line hand-rolled struct
    serverCache := newFilteredServerCache(sessionTimeout)
    return serverCache.getOrCreate(backendID, sessionID, func() *sdk.Server { ... })
    
    // After: generic TTLCache
    serverCache := syncutil.NewTTLCache[string, *sdk.Server](sessionTimeout, filteredServerCacheMaxSize)
    cacheKey := fmt.Sprintf("%s/%s", backendID, sessionID)
    return serverCache.GetOrCreate(cacheKey, func() *sdk.Server { ... })
  • internal/server/session_util.gotruncateCacheKeyForLog moved here alongside truncateSessionID, 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, and maxSize=1 edge case.

  • internal/server/routed_test.goTestFilteredServerCache_MaxSize and TestFilteredServerCache_TTLEviction removed; equivalent coverage migrated to syncutil/ttl_cache_test.go.

GitHub Advanced Security started work on behalf of lpcox June 9, 2026 15:37 View session
GitHub Advanced Security finished work on behalf of lpcox June 9, 2026 15:38
Copilot AI added 2 commits June 9, 2026 15:48
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.
GitHub Advanced Security started work on behalf of lpcox June 9, 2026 15:50 View session
Copilot AI changed the title [WIP] Fix duplicate code pattern in filteredServerCache refactor: replace filteredServerCache with generic syncutil.TTLCache Jun 9, 2026
Copilot finished work on behalf of lpcox June 9, 2026 15:50
Copilot AI requested a review from lpcox June 9, 2026 15:50
GitHub Advanced Security finished work on behalf of lpcox June 9, 2026 15:51
GitHub Advanced Security started work on behalf of lpcox June 9, 2026 15:51 View session
GitHub Advanced Security finished work on behalf of lpcox June 9, 2026 15:52
@lpcox lpcox marked this pull request as ready for review June 10, 2026 13:44
Copilot AI review requested due to automatic review settings June 10, 2026 13:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 generic TTLCache[K,V] with TTL + LRU eviction on GetOrCreate, plus injectable clock for deterministic tests.
  • Updated internal/server/routed.go to use syncutil.NewTTLCache[string, *sdk.Server] instead of the removed hand-rolled cache.
  • Added internal/syncutil/ttl_cache_test.go and 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

Comment thread internal/syncutil/ttl_cache.go Outdated
Comment thread internal/syncutil/ttl_cache.go Outdated
Comment thread internal/syncutil/ttl_cache.go
lpcox and others added 3 commits June 10, 2026 08:21
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>
@lpcox

lpcox commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Merged main into the branch. The only conflict was a modify/delete on internal/server/session_util.go — main had deleted it (moving truncateSessionID into session.go), while our branch had added truncateCacheKeyForLog there. Resolved by moving truncateCacheKeyForLog into session.go alongside truncateSessionID and deleting session_util.go. Build and tests pass.

Copilot finished work on behalf of lpcox June 10, 2026 17:47
@lpcox lpcox merged commit 7a6d7f4 into main Jun 10, 2026
26 checks passed
@lpcox lpcox deleted the copilot/aw-abc123def456-fix-duplicate-code branch June 10, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Pattern: filteredServerCache Re-implements Get-or-Create Pattern

3 participants