Langkau ke kandungan utama

Watchers

Watchers keep policy in sync across multiple enforcer instances. When one enforcer updates policy, it notifies others (via a message bus such as etcd, Redis, or Kafka), and they reload or update their in-memory policy. That lets you run many enforcers behind load balancers without stale permissions.

Watchers are implemented in separate packages, like adapters. The list below covers supported backends. To add a new one, open an issue or PR.

Watcher Type Author Description
PostgreSQL WatcherEx Database @IguteChung WatcherEx for PostgreSQL
Redis WatcherEx KV store Casbin WatcherEx for Redis
Redis Watcher KV store @billcobbler Watcher for Redis
Etcd Watcher KV store Casbin Watcher for etcd
TiKV Watcher KV store Casbin Watcher for TiKV
Kafka Watcher Messaging system @wgarunap Watcher for Apache Kafka
NATS Watcher Messaging system Soluto Watcher for NATS
ZooKeeper Watcher Messaging system Grepsr Watcher for Apache ZooKeeper
NATS, RabbitMQ, GCP Pub/Sub, AWS SNS & SQS, Kafka, InMemory Messaging System @rusenask Watcher based on Go Cloud Dev Kit that works with leading cloud providers and self-hosted infrastructure
NATS, RabbitMQ, GCP Pub/Sub, AWS SNS & SQS, Kafka, InMemory Messaging System @bartventer WatcherEx based on Go Cloud Dev Kit that works with leading cloud providers and self-hosted infrastructure
RocketMQ Watcher Messaging system @fmyxyz Watcher for Apache RocketMQ

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:

APIPenerangan
SetUpdateCallback(func(string)) ralatSetUpdateCallback configures the callback function that the watcher calls when the policy in the database has been changed by other instances. Panggilan balik klasik adalah Enforcer.LoadPolicy().
Kemaskini() ralatUpdate 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.
Tutup()Close stops and releases the watcher. The callback function will no longer be invoked after this.
UpdateForAddPolicy(sec, ptype string, params ...string) ralatUpdateForAddPolicy calls the update callback of other instances to synchronize their policies. Ia dipanggil selepas dasar ditambahkan melalui Enforcer.AddPolicy(), Enforcer.AddNamedPolicy(), Enforcer.AddGroupingPolicy() dan Enforcer.AddNamedGroupingPolicy().
UpdateForRemovePolicy(sec, ptype string, params ...string) errorUpdateForRemovePolicy calls the update callback of other instances to synchronize their policies. Ia dipanggil selepas dasar dikeluarkan oleh Enforcer.RemovePolicy(), Enforcer.RemoveNamedPolicy(), Enforcer.RemoveGroupingPolicy() dan Enforcer.RemoveNamedGroupingPolicy().
UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex int, fieldValues ...string) errorUpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policies. Ia dipanggil selepas Enforcer.RemoveFilteredPolicy(), Enforcer.RemoveFilteredNamedPolicy(), Enforcer.RemoveFilteredGroupingPolicy() dan Enforcer.RemoveFilteredNamedGroupingPolicy().
UpdateForSavePolicy(model model.Model) errorUpdateForSavePolicy calls the update callback of other instances to synchronize their policies. Ia dipanggil selepas Enforcer.SavePolicy().
UpdateForAddPolicies(sec string, ptype string, rules ...[]string) ralatUpdateForAddPolicies calls the update callback of other instances to synchronize their policies. Ia dipanggil selepas Enforcer.AddPolicies(), Enforcer.AddNamedPolicies(), Enforcer.AddGroupingPolicies() dan Enforcer.AddNamedGroupingPolicies().
UpdateForRemovePolicies(sec string, ptype string, rules ...[]string) ralatUpdateForRemovePolicies calls the update callback of other instances to synchronize their policies. It is called after Enforcer.RemovePolicies(), Enforcer.RemoveNamedPolicies(), Enforcer.RemoveGroupingPolicies() and Enforcer.RemoveNamedGroupingPolicies().

Comments are powered by giscus and GitHub Discussions. They load from giscus.app and GitHub only after you click the button below.