-
Notifications
You must be signed in to change notification settings - Fork 145
Description
vMCP: Add per-user caching to discovery manager
Problem
Issue #2502 implements lazy discovery that performs capability aggregation on every request. This works correctly with user authentication but has poor performance since every request queries all backends.
Solution
Add a simple in-memory cache directly to the DefaultManager. Cache keyed by (userID, backend set hash) with hardcoded 5-minute TTL and 1000 entry limit.
Changes
Modify pkg/vmcp/discovery/manager.go:
Add cache fields to DefaultManager:
- Map from cache key (string) to cache entry
- Cache entry contains capabilities and expiration timestamp
- Protect with sync.RWMutex
Update Discover() method:
- Extract user identity from context
- Build cache key from userID and hashed backend references
- Check cache, return if hit and not expired
- On miss, call aggregator and cache result
- Simple eviction: reject caching when at 1000 entry limit
Add cleanup:
- Background goroutine removes expired entries every minute
- Stop() method to terminate goroutine on shutdown
Implementation Notes
Hardcoded values: 5-minute TTL, 1000 entry max size, 1-minute cleanup interval. No configuration needed for MVP.
Cache key stability: Hash backend references after sorting to ensure consistent keys.
Thread safety: Use sync.RWMutex for all cache access.
Call Stop() on manager shutdown to prevent goroutine leaks.