Watchers
Maintaining consistency between multiple Casbin enforcer instances
We support distributed messaging systems like etcd to maintain consistency across multiple Casbin enforcer instances. This enables concurrent use of multiple Casbin enforcers to handle large volumes of permission checking requests.
Similar to policy storage adapters, watcher code is not included in the main library. Support for new messaging systems should be implemented as a separate watcher. Below is a complete list of available Casbin watchers. We welcome third-party contributions for new watchers—please let us know, and we'll add them to this list.
WatcherEx
To support incremental synchronization between multiple instances, we provide the WatcherEx interface. This interface can notify other instances when policies change, though there is currently no implementation of WatcherEx. We recommend using a dispatcher to achieve this functionality.
Compared to the Watcher interface, WatcherEx can distinguish the type of update action received, such as AddPolicy versus RemovePolicy.
WatcherEx APIs:
| API | Description |
|---|---|
| SetUpdateCallback(func(string)) error | SetUpdateCallback configures the callback function that the watcher calls when the policy in the database has been changed by other instances. A classic callback is Enforcer.LoadPolicy(). |
| Update() error | Update calls the update callback of other instances to synchronize their policies. It is usually called after changing the policy in the database, such as after Enforcer.SavePolicy(), Enforcer.AddPolicy(), Enforcer.RemovePolicy(), etc. |
| Close() | Close stops and releases the watcher. The callback function will no longer be invoked after this. |
| UpdateForAddPolicy(sec, ptype string, params ...string) error | UpdateForAddPolicy calls the update callback of other instances to synchronize their policies. It is called after a policy is added via Enforcer.AddPolicy(), Enforcer.AddNamedPolicy(), Enforcer.AddGroupingPolicy() and Enforcer.AddNamedGroupingPolicy(). |
| UpdateForRemovePolicy(sec, ptype string, params ...string) error | UpdateForRemovePolicy calls the update callback of other instances to synchronize their policies. It is called after a policy is removed by Enforcer.RemovePolicy(), Enforcer.RemoveNamedPolicy(), Enforcer.RemoveGroupingPolicy() and Enforcer.RemoveNamedGroupingPolicy(). |
| UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex int, fieldValues ...string) error | UpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policies. It is called after Enforcer.RemoveFilteredPolicy(), Enforcer.RemoveFilteredNamedPolicy(), Enforcer.RemoveFilteredGroupingPolicy() and Enforcer.RemoveFilteredNamedGroupingPolicy(). |
| UpdateForSavePolicy(model model.Model) error | UpdateForSavePolicy calls the update callback of other instances to synchronize their policies. It is called after Enforcer.SavePolicy(). |
| UpdateForAddPolicies(sec string, ptype string, rules ...[]string) error | UpdateForAddPolicies calls the update callback of other instances to synchronize their policies. It is called after Enforcer.AddPolicies(), Enforcer.AddNamedPolicies(), Enforcer.AddGroupingPolicies() and Enforcer.AddNamedGroupingPolicies(). |
| UpdateForRemovePolicies(sec string, ptype string, rules ...[]string) error | UpdateForRemovePolicies calls the update callback of other instances to synchronize their policies. It is called after Enforcer.RemovePolicies(), Enforcer.RemoveNamedPolicies(), Enforcer.RemoveGroupingPolicies() and Enforcer.RemoveNamedGroupingPolicies(). |
