Conversation
…, #296) Two related RBAC fixes for Enterprise GA: 1. Goroutine leak (#295): cacheCleanupLoop ran in an infinite loop with no shutdown mechanism. Added a done channel and Close() method following the AuthManager pattern. RBACManager is now registered with the shutdown coordinator at PriorityAuth. 2. Unbounded caches (#296): both tokenCache and permCache had no maximum size — only TTL-based expiration. Added configurable MaxCacheSize (default 10,000 entries per cache) with random eviction on insertion when the cache exceeds its limit.
There was a problem hiding this comment.
Code Review
This pull request fixes a goroutine leak in RBACManager by adding a Close method and a shutdown signal, and prevents unbounded memory growth by implementing a maximum cache size with random eviction. Feedback was provided to extract the duplicated eviction logic into a reusable helper method to improve code maintainability.
| if len(rm.permCache) >= rm.maxCacheSize { | ||
| for k := range rm.permCache { | ||
| delete(rm.permCache, k) | ||
| break | ||
| } | ||
| } |
There was a problem hiding this comment.
This cache eviction logic is duplicated in CheckPermissionsBatch (lines 1059-1064) and for tokenCache in getTokenRBACData (lines 1142-1147). To improve maintainability and reduce code duplication, consider extracting this logic into a private helper method. This would centralize the eviction strategy, making future modifications (e.g., switching to an LRU policy) much simpler.
Extracted evictPermCacheIfFull() and evictTokenCacheIfFull() to centralize the eviction strategy across the 3 insertion points. Makes future changes (e.g. switching to LRU) a single-point modification.
Summary
cacheCleanupLoopgoroutine ran forever with no shutdown mechanism. Addeddonechannel +Close()method following the AuthManager pattern. Registered with shutdown coordinator atPriorityAuth.tokenCacheandpermCachehad no max size — only TTL-based expiration. Added configurableMaxCacheSize(default 10,000 entries per cache) with random eviction on insertion when exceeded.Test plan
go build ./cmd/... ./internal/...passesgo test ./internal/auth/...passesCloses #295
Closes #296