Skip to content

Prevent infinite loops in distributed watchers by bypassing adapter in self* methods #537

@Huggin423

Description

@Huggin423

Goal: Modify the Enforcer to allow updating the policy in memory without triggering the Adapter (database write) or the Watcher (notification), even when autoSave is enabled.

Detailed Implementation Plan:

Modify InternalEnforcer methods:

Update all internal modification methods (e.g., addPolicyInternal, removePolicyInternal, updatePolicyInternal, addPoliciesInternal, etc.).

Add a new optional parameter useAdapter: boolean to these methods.

Set the default value of useAdapter to true to ensure backward compatibility.

In the method body, wrap the adapter call logic (e.g., this.adapter.addPolicy(...)) with a check: if (useAdapter && this.adapter && this.autoSave).

Update ManagementEnforcer (self* methods):

Locate all self* methods (e.g., selfAddPolicy, selfRemovePolicy, selfUpdatePolicy, etc.).

Update their implementation to call the corresponding *Internal method passing false for the new useAdapter parameter (along with false for the existing useWatcher parameter).

This ensures that calling self* methods will strictly update the memory model only, bypassing both the database adapter and the watcher notification.

Documentation:

Add JSDoc comments to these self* methods explaining that they are intended for in-memory updates in distributed setups (e.g., inside a Watcher callback).

Expected Outcome: Users can call enforcer.selfAddPolicy(...) in a Watcher callback. This will update the local cache but prevent the dead-loop caused by writing back to the DB and triggering the Watcher again.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions