watcher

package module
v0.0.0-...-12bea1d Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

Titv-Watcher

Tikv-Watcher is the Kitv watcher for casbin, With this library, casbin Casbin can synchronize the policy with the database in multiple enforcer instances.

Note: Considering that tikv doesn't have watch mechanism like etcd or channel like redis, this ugly implementation uses polling to achieve monitoring a certain key, which may cause some performance trouble

Installation:
go get github.com/ComradeProgrammer/casbin-tikv-watcher
Single Example:

start the tikv service before run this example

package main

import (
	"fmt"
	"time"

	watcher "github.com/ComradeProgrammer/casbin-tikv-watcher"
	casbin "github.com/casbin/casbin/v2"
)

func main() {
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
	w, err := watcher.NewWatcher(
		"testkey",
		100*time.Millisecond,
		"127.0.0.1:2379",
	)
	if err != nil {
		fmt.Println(err)
		return
	}
	e.SetWatcher(w)
	w.SetUpdateCallback(
		func(s string) {
			fmt.Println("===================get" + s)
		},
	)
	e.SavePolicy()
	time.Sleep(10 * time.Second)

}


License:

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Watcher

type Watcher struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewWatcher

func NewWatcher(keyName string, pollInterval time.Duration, pdAddrs string) (*Watcher, error)

func (*Watcher) Close

func (w *Watcher) Close()

func (*Watcher) SetUpdateCallback

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

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

func (w *Watcher) Update() error

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.

Jump to

Keyboard shortcuts

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