fix: CachedEnforcer WatcherEx callback broken — causes stale cache in multi-instance setups#1736
Open
daviediao-code wants to merge 3 commits into
Open
fix: CachedEnforcer WatcherEx callback broken — causes stale cache in multi-instance setups#1736daviediao-code wants to merge 3 commits into
daviediao-code wants to merge 3 commits into
Conversation
…ache invalidation When SetWatcher is called with a WatcherEx implementation (e.g., Redis watcher), the callback was previously not set, causing distributed cache invalidation to silently fail. This meant that when another instance modified the policy, the local CachedEnforcer would continue returning stale cached decisions. Fix: - Override SetWatcher in CachedEnforcer to always set a callback, even for WatcherEx implementations - The callback calls InvalidateCache() + LoadPolicy() for efficient cache clearing on policy changes - Same fix applied to SyncedCachedEnforcer - Add comprehensive tests for WatcherEx callback behavior Co-Authored-By: davie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
CachedEnforceris used with a WatcherEx (e.g., Redis watcher for distributed cache invalidation), theSetWatcher()callback is never set. This means when another instance modifies the policy, the cache is never invalidated, and stale decisions are returned forever.Root Cause
Enforcer.SetWatcher()(enforcer.go:249) explicitly skips setting a callback for WatcherEx implementations.Fix
Override
SetWatcher()in bothCachedEnforcerandSyncedCachedEnforcerto set a callback even for WatcherEx. The callback callsInvalidateCache()+LoadPolicy().Files Changed
enforcer_cached.go: SetWatcher override with WatcherEx-aware callbackenforcer_cached_synced.go: Same fix for SyncedCachedEnforcerenforcer_cached_watcher_test.go: 5 new testsTests
All existing tests pass (18s). New tests cover WatcherEx callback, basic Watcher callback, direct InvalidateCache, and TTL behavior.
PR #1729