rediswatcher

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

Redis Watcher Build Status

Redis Watcher is a Redis watcher for Casbin.

Installation

go get github.com/billcobbler/casbin-redis-watcher

Simple Example

package main

import (
    "github.com/casbin/casbin"
    "github.com/casbin/casbin/util"
    "github.com/billcobbler/casbin-redis-watcher"
)

func updateCallback(msg string) {
    util.LogPrint(msg)
}

func main() {
    // Initialize the watcher.
    // Use the Redis host as parameter.
    w, _ := rediswatcher.NewWatcher("127.0.0.1:6379")
    
    // Initialize the enforcer.
    e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
    
    // Set the watcher for the enforcer.
    e.SetWatcher(w)
    
    // Set callback to local example
    w.SetUpdateCallback(updateCallback)
    
    // Update the policy to test the effect.
    // You should see "[casbin rules updated]" in the log.
    e.SavePolicy()
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const (
	RedisDoAuthMetric       = "RedisDoAuth"
	RedisCloseMetric        = "RedisClose"
	RedisDialMetric         = "RedisDial"
	PubSubPublishMetric     = "PubSubPublish"
	PubSubReceiveMetric     = "PubSubReceive"
	PubSubSubscribeMetric   = "PubSubSubscribe"
	PubSubUnsubscribeMetric = "PubSubUnsubscribe"
)

Variables

This section is empty.

Functions

func IsCallbackPending

func IsCallbackPending(w *Watcher, shouldClear bool) bool

IsCallbackPending

func NewPublishWatcher

func NewPublishWatcher(addr string, setters ...WatcherOption) (persist.Watcher, error)

NewPublishWatcher return a Watcher only publish but not subscribe

func NewWatcher

func NewWatcher(addr string, setters ...WatcherOption) (persist.Watcher, error)

NewWatcher creates a new Watcher to be used with a Casbin enforcer addr is a redis target string in the format "host:port" setters allows for inline WatcherOptions

Example:
		w, err := rediswatcher.NewWatcher("127.0.0.1:6379", rediswatcher.Password("pass"), rediswatcher.Channel("/yourchan"))

A custom redis.Conn can be provided to NewWatcher

Example:
		c, err := redis.Dial("tcp", ":6379")
		w, err := rediswatcher.NewWatcher("", rediswatcher.WithRedisConnection(c)

Types

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

func (*Watcher) Close

func (w *Watcher) Close()

Close disconnects the watcher from redis

func (*Watcher) GetWatcherOptions

func (w *Watcher) GetWatcherOptions() WatcherOptions

return option settings

func (*Watcher) SetUpdateCallback

func (w *Watcher) SetUpdateCallback(callback func(string)) error

SetUpdateCallBack sets the update callback function invoked by the watcher when the policy is updated. Defaults to Enforcer.LoadPolicy()

func (*Watcher) Update

func (w *Watcher) Update() error

Update publishes a message to all other casbin instances telling them to invoke their update callback

type WatcherMetrics

type WatcherMetrics struct {
	Name        string
	LatencyMs   float64
	LocalID     string
	Channel     string
	Protocol    string
	Error       error
	MessageSize int64
}

type WatcherOption

type WatcherOption func(*WatcherOptions)

func Channel

func Channel(subject string) WatcherOption

func IgnoreSelf

func IgnoreSelf(ignore bool) WatcherOption

func LocalID

func LocalID(id string) WatcherOption

func Password

func Password(password string) WatcherOption

func Protocol

func Protocol(protocol string) WatcherOption

func RecordMetrics

func RecordMetrics(callback func(*WatcherMetrics)) WatcherOption

func SquashMessages

func SquashMessages(squash bool) WatcherOption

func SquashTimeoutLong

func SquashTimeoutLong(d time.Duration) WatcherOption

func SquashTimeoutShort

func SquashTimeoutShort(d time.Duration) WatcherOption

func Username

func Username(username string) WatcherOption

func WithRedisPubConnection

func WithRedisPubConnection(connection redis.Conn) WatcherOption

func WithRedisSubConnection

func WithRedisSubConnection(connection redis.Conn) WatcherOption

type WatcherOptions

type WatcherOptions struct {
	Channel            string
	PubConn            redis.Conn
	SubConn            redis.Conn
	Username           string
	Password           string
	Protocol           string
	IgnoreSelf         bool
	LocalID            string
	RecordMetrics      func(*WatcherMetrics)
	SquashMessages     bool
	SquashTimeoutShort time.Duration
	SquashTimeoutLong  time.Duration
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL