跳转至主要内容

监视器

我们支持使用分布式消息系统,例如 etcd 来保持多个Casbin执行器实例之间的一致性。 因此,我们的用户可以同时使用多个Casbin 执行器来处理大量的权限检查请求。

与策略存储适配器类似,我们没有把watcher的代码放在主库中。 任何对新消息系统的支持都应该作为watcher程序来实现。 完整的Casbin watchers列表如下所示。 欢迎任何第三方对 watcher做出新的贡献,如果有请告知我们,我将把它放在这个列表中:)

Watcher类型作者描述
PostgreSQL WatcherExDatabase@IguteChungWatcherEx for PostgreSQL
Redis WatcherExKV storeCasbinWatcherEx for Redis
Redis WatcherKV store@billcobblerRedis的监视器
Etcd WatcherKV storeCasbinetcd的监视器
TiKV 监视器KV storeCasbinTiKV 的监视器
Kafka 监视器Messaging system@wgarunapApache Kafka的监视器
NATS WatcherMessaging systemSolutoNATS的监视器
ZooKeeper WatcherMessaging systemGrepsrApache ZooKeeper的监视器
NATS, RabbitMQ, GCP Pub/Sub, AWS SNS & SQS, Kafka, InMemoryMessaging System@rusenask监视器基于与领先的云供应商和自托管基础设施运作的Go Cloud Dev Kit
RocketMQ 观察器Messaging system@fmyxyzApache RocketMQ的监视器

WatcherEx

为了支持多个实例之间的增量同步,我们提供 WatcherEx 接口。 我们希望它能够在策略改变时通知其他实例,但目前尚未实现 WatcherEx。 我们推荐您使用调度器来实现它。

个监视器 接口相比, WatcherEx 可以区分收到的更新动作类型。 添加策略删除策略.

WatcherEx 的调用接口:

应用程序接口描述
SetUpdateCallback(func(string)) errorSetUpdateCallback sets the callback function that the watcher will call, when the policy in DB has been changed by other instances. 一种典型的回调是Enforcer.LoadPolicy()。
Update() errorUpdate calls the update callback of other instances to synchronize their policy. 它通常是在改变数据库中的策略之后进行的,如Enforcer.SavePolicy(), Enforcer.AddPolicy(), Enforcer.RemovePolicy()等。
Close()Close stops and releases the watcher, the callback function will not be called any more.
UpdateForAddPolicy(sec, ptype string, params ...string) errorUpdateForAddPolicy calls the update callback of other instances to synchronize their policy. 它是在 Enforcer.AddPolicy(), Enforcer.AddNamedPolicy(), Enforcer.AddGroupingPolicy() 和 Enforcer.AddNamedGroupingPolicy() 后被调用。
UpdateForRemovePolicy(sec, ptype string, params ...string) errorUPdateForRemovePolicy calls the update callback of other instances to synchronize their policy. 它是在政策被Enforcer.RemovePolicy(), Enforcer.RemoveNamedPolicy(), Enforcer.RemoveGroupingPolicy() 和 Enforcer.RemoveNamedGroupingPolicy() 删除后被调用。
UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex int, fieldValues ...string) errorUpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy. 它是在Enforcer.RemoveFilteredPolicy(), Enforcer.RemoveFilteredNamedPolicy(), Enforcer.RemoveFilteredGroupingPolicy() 和 Enforcer.RemoveFilteredNamedGroupingPolicy()之后被调用
UpdateForSavePolicy(model model.Model) errorUpdateForSavePolicy calls the update callback of other instances to synchronize their policy. 它在 Enforcer.AddPolicy()被调用后调用
UpdateForAddPolicies(sec string, ptype string, rules ...[]string) errorUpdateForAddPolicies calls the update callback of other instances to synchronize their policy. 它是在Enforcer.AddPolicies(), Enforcecer.AddNamedPolicies(), Enforcer.AddGroupingPolicies() 和 Enforcer.AddNamedGroupingPolicies()之后被调用
UpdateForRemovePolicies(sec string, ptype string, rules ...[]string) errorUpdateForRemovePolicies calls the update callback of other instances to synchronize their policy. 它是在Enforcer.RemovePolicies(), Enforcecer.RemoveNamedPolicies(), Enforcer.RemoveGroupingPolicies() 和 Enforcer.RemoveNamedGroupingPolicies() 后被调用。