-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
187 lines (165 loc) · 3.97 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package rediswatcher
import (
"fmt"
"github.com/casbin/casbin/v2/persist"
"reflect"
"runtime"
"sync"
"github.com/go-redis/redis"
)
type Watcher struct {
options WatcherOptions
pubConn *redis.Client //生产消息
subConn *redis.Client //订阅消息
callback func(string)
closed chan struct{}
once sync.Once
}
//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)
////
func NewWatcher(addr string, setters ...WatcherOption) (persist.Watcher, error) {
w := &Watcher{
closed: make(chan struct{}),
}
w.options = WatcherOptions{
Channel: "/casbin",
Protocol: "tcp",
}
// call destructor when the object is released
runtime.SetFinalizer(w, finalizer)
for _, setter := range setters {
setter(&w.options)
}
if err := w.connect(addr); err != nil {
return nil, err
}
go func() {
for {
select {
case <-w.closed:
return
default:
err := w.subscribe()
if err != nil {
fmt.Printf("Failure from Redis subscription: %v", err)
}
}
}
}()
return w, nil
}
// SetUpdateCallBack sets the update callback function invoked by the watcher
// when the policy is updated. Defaults to Enforcer.LoadPolicy()
func (w *Watcher) SetUpdateCallback(callback func(string)) error {
w.callback = callback
return nil
}
// Update publishes a message to all other casbin instances telling them to
// invoke their update callback
func (w *Watcher) Update() error {
if _, err := w.pubConn.Publish(w.options.Channel, "casbin rules updated").Result(); err != nil {
return err
}
return nil
//if _, err := w.pubConn.Do("PUBLISH", w.options.Channel, "casbin rules updated").Result(); err != nil {
// return err
//}
//return nil
}
// Close disconnects the watcher from redis
func (w *Watcher) Close() {
finalizer(w)
}
func (w *Watcher) connect(addr string) error {
if err := w.connectPub(addr); err != nil {
return err
}
if err := w.connectSub(addr); err != nil {
return err
}
return nil
}
func (w *Watcher) connectPub(addr string) error {
if w.options.PubConn != nil {
w.pubConn = w.options.PubConn
return nil
}
client := redis.NewClient(
&redis.Options{
Addr: addr,
Password: w.options.Password,
DB: 0,
})
if _, err := client.Ping().Result(); err != nil {
fmt.Printf("connectPub error[%s]\n", err.Error())
return err
}
w.pubConn = client
return nil
}
func (w *Watcher) connectSub(addr string) error {
if w.options.SubConn != nil {
w.subConn = w.options.SubConn
return nil
}
client := redis.NewClient(
&redis.Options{
Addr: addr,
Password: w.options.Password,
DB: 0,
})
if _, err := client.Ping().Result(); err != nil {
fmt.Printf("connectSub error[%s]\n", err.Error())
return err
}
w.subConn = client
return nil
}
func (w *Watcher) subscribe() error {
psc := w.subConn.Subscribe(w.options.Channel)
//if _, err := psc.Receive(); err != nil {
// fmt.Printf("try subscribe channel[%s] error[%s]\n", w.options.Channel, err.Error())
// return nil
//}
defer psc.Unsubscribe()
for {
receive, err := psc.Receive()
fmt.Println(reflect.TypeOf(receive))
if err != nil {
fmt.Printf("try subscribe channel[%s] error[%s]\n", w.options.Channel, err.Error())
return nil
}
switch n := receive.(type) {
case error:
return n
case *redis.Message:
fmt.Printf("有没有进到这儿呢:%v \n", w.callback)
if w.callback != nil {
w.callback("success")
}
case *redis.Subscription:
if n.Count == 0 {
return nil
}
}
}
return nil
}
func finalizer(w *Watcher) {
w.once.Do(func() {
close(w.closed)
w.subConn.Close()
w.pubConn.Close()
})
}