@@ -24,8 +24,8 @@ import (
2424//go:generate mockgen -destination=mocks/mock_manager.go -package=mocks -source=manager.go Manager
2525
2626const (
27- // cacheTTL is the time-to-live for cached capability entries.
28- cacheTTL = 5 * time .Minute
27+ // DefaultCacheTTL is the default time-to-live for cached capability entries.
28+ DefaultCacheTTL = 5 * time .Minute
2929 // maxCacheSize is the maximum number of entries allowed in the cache.
3030 maxCacheSize = 1000
3131 // cleanupInterval is how often expired cache entries are removed.
@@ -59,21 +59,33 @@ type cacheEntry struct {
5959type DefaultManager struct {
6060 aggregator aggregator.Aggregator
6161 cache map [string ]* cacheEntry
62+ cacheTTL time.Duration
6263 cacheMu sync.RWMutex
6364 stopCh chan struct {}
6465 stopOnce sync.Once
6566 wg sync.WaitGroup
6667}
6768
68- // NewManager creates a new discovery manager with the given aggregator.
69+ // NewManager creates a new discovery manager with the given aggregator using the default cache TTL .
6970func NewManager (agg aggregator.Aggregator ) (Manager , error ) {
71+ return NewManagerWithTTL (agg , DefaultCacheTTL )
72+ }
73+
74+ // NewManagerWithTTL creates a new discovery manager with the given aggregator and cache TTL.
75+ // Use this when you need custom cache TTL (e.g., shorter TTL for testing).
76+ func NewManagerWithTTL (agg aggregator.Aggregator , cacheTTL time.Duration ) (Manager , error ) {
7077 if agg == nil {
7178 return nil , ErrAggregatorNil
7279 }
7380
81+ if cacheTTL <= 0 {
82+ cacheTTL = DefaultCacheTTL
83+ }
84+
7485 m := & DefaultManager {
7586 aggregator : agg ,
7687 cache : make (map [string ]* cacheEntry ),
88+ cacheTTL : cacheTTL ,
7789 stopCh : make (chan struct {}),
7890 }
7991
@@ -184,7 +196,7 @@ func (m *DefaultManager) cacheCapabilities(key string, caps *aggregator.Aggregat
184196
185197 m .cache [key ] = & cacheEntry {
186198 capabilities : caps ,
187- expiresAt : time .Now ().Add (cacheTTL ),
199+ expiresAt : time .Now ().Add (m . cacheTTL ),
188200 }
189201}
190202
0 commit comments