Documentation ¶
Overview ¶
Package watcher provides an implementation of persist.WatcherEx, supporting various pub/sub systems by leveraging the gocloud pubsub package.
For more details about the pub/sub systems supported by gocloud, please refer to https://gocloud.dev/howto/pubsub/.
Index ¶
- Variables
- func DefaultCallback(e casbin.IEnforcer) func(string)
- type MSG
- type Option
- type UpdateType
- type Watcher
- func (w *Watcher) Close()
- func (w *Watcher) GetLocalID() string
- func (w *Watcher) GetVerbose() bool
- func (w *Watcher) SetUpdateCallback(callbackFunc func(string)) error
- func (w *Watcher) Update() error
- func (w *Watcher) UpdateForAddPolicies(sec string, ptype string, rules ...[]string) error
- func (w *Watcher) UpdateForAddPolicy(sec string, ptype string, params ...string) error
- func (w *Watcher) UpdateForRemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
- func (w *Watcher) UpdateForRemovePolicies(sec string, ptype string, rules ...[]string) error
- func (w *Watcher) UpdateForRemovePolicy(sec string, ptype string, params ...string) error
- func (w *Watcher) UpdateForSavePolicy(model model.Model) error
- func (w *Watcher) UpdateForUpdatePolicies(sec string, ptype string, oldRules, newRules [][]string) error
- func (w *Watcher) UpdateForUpdatePolicy(sec string, ptype string, oldRule, newRule []string) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotConnected is the error returned when the watcher is not connected. ErrNotConnected = errors.New("pubsub not connected, cannot dispatch update message") )
Functions ¶
func DefaultCallback ¶
func DefaultCallback(e casbin.IEnforcer) func(string)
DefaultCallback is the default callback function that the watcher will call when the policy in DB has been changed by other instances.
Types ¶
type MSG ¶
type MSG struct { Method UpdateType `json:"method"` // Type of update. ID string `json:"id"` // Unique ID of the watcher instance. Sec string `json:"sec,omitempty"` // Section of the policy being updated. Ptype string `json:"ptype,omitempty"` // Type of policy being updated. OldRules [][]string `json:"old_rules,omitempty"` // Previous state of the policy rules. NewRules [][]string `json:"new_rules,omitempty"` // New state of the policy rules. FieldIndex int `json:"field_index,omitempty"` // Index of the field being updated. FieldValues []string `json:"field_values,omitempty"` // Values of the field being updated. }
MSG represents the payload for a pub/sub message, detailing the type of update and the specifics of the policy change.
func (MSG) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (*MSG) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
type Option ¶
type Option struct { Verbose bool // Verbose indicates the watcher should print verbose log. LocalID string // LocalID indicates the watcher's local ID, used to ignore self update event. Generates a random id if not specified. }
Option is the option for the watcher.
type UpdateType ¶
type UpdateType string
UpdateType is the type of update.
const ( Update UpdateType = "Update" UpdateForAddPolicy UpdateType = "UpdateForAddPolicy" UpdateForRemovePolicy UpdateType = "UpdateForRemovePolicy" UpdateForRemoveFilteredPolicy UpdateType = "UpdateForRemoveFilteredPolicy" UpdateForSavePolicy UpdateType = "UpdateForSavePolicy" UpdateForAddPolicies UpdateType = "UpdateForAddPolicies" UpdateForRemovePolicies UpdateType = "UpdateForRemovePolicies" UpdateForUpdatePolicy UpdateType = "UpdateForUpdatePolicy" UpdateForUpdatePolicies UpdateType = "UpdateForUpdatePolicies" )
Defines the update types.
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher implements Casbin updates watcher to synchronize policy changes between the nodes.
func New ¶
New creates a new watcher
Parameters:
- ctx: the context for pubsub connections
- url: the pubsub url (e.g. "kafka://my-topic")
Returns:
- Watcher: the new watcher instance
- error: the error if the watcher cannot be created
func NewWithOption ¶
NewWithOption creates a new watcher with the option
Parameters:
- ctx: the context for pubsub connections
- url: the pubsub url (e.g. "kafka://my-topic")
- opt: the watcher option
Returns:
- Watcher: the new watcher instance
- error: the error if the watcher cannot be created
func (*Watcher) Close ¶
func (w *Watcher) Close()
Close stops and releases the watcher, the callback function will not be called any more.
func (*Watcher) GetLocalID ¶
GetLocalID gets the local id for the option.
func (*Watcher) GetVerbose ¶
GetVerbose gets the verbose for the option.
func (*Watcher) SetUpdateCallback ¶
SetUpdateCallback sets the callback function that the watcher will call when the policy in DB has been changed by other instances. A classic callback is Enforcer.LoadPolicy().
func (*Watcher) Update ¶
Update calls the update callback of other instances to synchronize their policy. It is usually called after changing the policy in DB, like Enforcer.SavePolicy(), Enforcer.AddPolicy(), Enforcer.RemovePolicy(), etc.
func (*Watcher) UpdateForAddPolicies ¶
UpdateForAddPolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.AddPolicies(), Enforcer.AddNamedPolicies(), Enforcer.AddGroupingPolicies() and Enforcer.AddNamedGroupingPolicies().
func (*Watcher) UpdateForAddPolicy ¶
UpdateForAddPolicy calls the update callback of other instances to synchronize their policy. It is called after a policy is added via Enforcer.AddPolicy(), Enforcer.AddNamedPolicy(), Enforcer.AddGroupingPolicy() and Enforcer.AddNamedGroupingPolicy().
func (*Watcher) UpdateForRemoveFilteredPolicy ¶
func (w *Watcher) UpdateForRemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
UpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredPolicy(), Enforcer.RemoveFilteredNamedPolicy(), Enforcer.RemoveFilteredGroupingPolicy() and Enforcer.RemoveFilteredNamedGroupingPolicy().
func (*Watcher) UpdateForRemovePolicies ¶
UpdateForRemovePolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemovePolicies(), Enforcer.RemoveNamedPolicies(), Enforcer.RemoveGroupingPolicies() and Enforcer.RemoveNamedGroupingPolicies().
func (*Watcher) UpdateForRemovePolicy ¶
UPdateForRemovePolicy calls the update callback of other instances to synchronize their policy. It is called after a policy is removed by Enforcer.RemovePolicy(), Enforcer.RemoveNamedPolicy(), Enforcer.RemoveGroupingPolicy() and Enforcer.RemoveNamedGroupingPolicy().
func (*Watcher) UpdateForSavePolicy ¶
UpdateForSavePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.SavePolicy().