-
Notifications
You must be signed in to change notification settings - Fork 5
/
watcher.go
144 lines (122 loc) · 3.09 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
package watcher
import (
"context"
"errors"
"fmt"
"log"
"runtime"
"sync"
"time"
"github.com/casbin/casbin/persist"
"gocloud.dev/pubsub"
)
// check interface compatibility
var _ persist.Watcher = &Watcher{}
// Errors
var (
ErrNotConnected = errors.New("pubsub not connected, cannot dispatch update message")
)
// Watcher implements Casbin updates watcher to synchronize policy changes
// between the nodes
type Watcher struct {
url string
callbackFunc func(string)
connMu *sync.RWMutex
ctx context.Context
topic *pubsub.Topic
sub *pubsub.Subscription
}
// New creates a new watcher https://gocloud.dev/concepts/urls/
// gcppubsub://myproject/mytopic
func New(ctx context.Context, url string) (*Watcher, error) {
w := &Watcher{
url: url,
connMu: &sync.RWMutex{},
}
runtime.SetFinalizer(w, finalizer)
err := w.initializeConnections(ctx)
return w, err
}
// 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 (w *Watcher) SetUpdateCallback(callbackFunc func(string)) error {
w.connMu.Lock()
w.callbackFunc = callbackFunc
w.connMu.Unlock()
return nil
}
func (w *Watcher) initializeConnections(ctx context.Context) error {
w.connMu.Lock()
defer w.connMu.Unlock()
w.ctx = ctx
topic, err := pubsub.OpenTopic(ctx, w.url)
if err != nil {
return err
}
w.topic = topic
return w.subscribeToUpdates(ctx)
}
func (w *Watcher) subscribeToUpdates(ctx context.Context) error {
sub, err := pubsub.OpenSubscription(ctx, w.url)
if err != nil {
return fmt.Errorf("failed to open updates subscription, error: %w", err)
}
w.sub = sub
go func() {
for {
msg, err := sub.Receive(ctx)
if err != nil {
if ctx.Err() == context.Canceled {
// nothing to do
return
}
log.Printf("Error while receiving an update message: %s\n", err)
return
}
w.executeCallback(msg)
msg.Ack()
}
}()
return nil
}
func (w *Watcher) executeCallback(msg *pubsub.Message) {
w.connMu.RLock()
defer w.connMu.RUnlock()
if w.callbackFunc != nil {
go w.callbackFunc(string(msg.Body))
}
}
// 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 (w *Watcher) Update() error {
w.connMu.RLock()
defer w.connMu.RUnlock()
if w.topic == nil {
return ErrNotConnected
}
m := &pubsub.Message{Body: []byte("")}
return w.topic.Send(w.ctx, m)
}
// Close stops and releases the watcher, the callback function will not be called any more.
func (w *Watcher) Close() {
finalizer(w)
}
func finalizer(w *Watcher) {
w.connMu.Lock()
defer w.connMu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if w.topic != nil {
w.topic = nil
}
if w.sub != nil {
err := w.sub.Shutdown(ctx)
if err != nil {
log.Printf("Subscription shutdown failed, error: %s\n", err)
}
w.sub = nil
}
w.callbackFunc = nil
}