Skip to content

refactor: replace filteredServerCache with generic syncutil.TTLCache#7277

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/aw-abc123def456-fix-duplicate-code
Draft

refactor: replace filteredServerCache with generic syncutil.TTLCache#7277
Copilot wants to merge 3 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
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

2 participants