/
watcher.go
168 lines (138 loc) · 3.54 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
package nuts
import (
"bytes"
"github.com/RealFax/RedQueen/internal/rqd/store"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
const WatcherNotifyBufferSize = 32
type WatcherNotifier struct {
state atomic.Bool
unwatchedFunc func()
Values chan *store.WatchValue
UUID string
}
func (n *WatcherNotifier) Notify() chan *store.WatchValue {
return n.Values
}
func (n *WatcherNotifier) Close() error {
if !n.state.Load() {
return errors.New("watcher notify has closed")
}
n.state.Store(false)
n.unwatchedFunc()
close(n.Values)
return nil
}
type WatcherChannel struct {
mu sync.Mutex
PrefixWatch bool
sequence uint64
lastUpdate int64
Value *[]byte
Prefix []byte
Notify sync.Map // map[string]*WatcherNotifier
}
func (c *WatcherChannel) AddNotifier(dest *WatcherNotifier) {
dest.state = atomic.Bool{}
dest.state.Store(true)
dest.unwatchedFunc = func() {
c.Notify.Delete(dest.UUID)
}
c.Notify.Store(dest.UUID, dest)
}
func (c *WatcherChannel) TryUpdate(key []byte, value *[]byte, ttl uint32) {
if c.PrefixWatch && !bytes.HasPrefix(key, c.Prefix) {
return
}
c.mu.Lock()
c.sequence++
c.lastUpdate = time.Now().UnixMilli()
c.Value = value
seq := c.sequence
timestamp := c.lastUpdate
c.mu.Unlock()
watchValue := &store.WatchValue{
Seq: seq,
Timestamp: timestamp,
TTL: ttl,
Key: key,
Value: value,
}
c.Notify.Range(func(_, value any) bool {
dest := value.(*WatcherNotifier)
// chan is full
if len(dest.Values) == cap(dest.Values) {
return true
}
dest.Values <- watchValue
return true
})
}
type WatcherChild struct {
Namespace string
Channels sync.Map // map[uint64]*WatcherChannel
PrefixChannels sync.Map // map[string]*WatcherChannel
}
func (c *WatcherChild) Watch(key []byte) *WatcherNotifier {
watchKey := WatchKey(key)
channel, ok := c.Channels.Load(watchKey)
if !ok {
// init watcher channel
channel, _ = c.Channels.LoadOrStore(watchKey, &WatcherChannel{})
}
// create watcher notify
notify := &WatcherNotifier{
Values: make(chan *store.WatchValue, WatcherNotifyBufferSize),
UUID: uuid.New().String(),
}
// setup watcher notify
channel.(*WatcherChannel).AddNotifier(notify)
return notify
}
func (c *WatcherChild) WatchPrefix(prefix []byte) *WatcherNotifier {
prefixKey := PrefixKey(prefix)
channel, ok := c.PrefixChannels.Load(prefixKey)
if !ok {
// init prefix watcher channel
channel, _ = c.PrefixChannels.LoadOrStore(prefixKey, &WatcherChannel{PrefixWatch: true, Prefix: prefix})
}
// create prefix watcher notify
notify := &WatcherNotifier{
Values: make(chan *store.WatchValue, WatcherNotifyBufferSize),
UUID: uuid.New().String(),
}
// setup prefix watcher notify
channel.(*WatcherChannel).AddNotifier(notify)
return notify
}
func (c *WatcherChild) Update(key, value []byte, ttl uint32) {
var pValue *[]byte
if value != nil {
pValue = &value
}
// update prefix channels value
c.PrefixChannels.Range(func(_, value any) bool {
channel, _ := value.(*WatcherChannel)
channel.TryUpdate(key, pValue, ttl)
return true
})
channel, ok := c.Channels.Load(WatchKey(key))
if !ok {
return
}
channel.(*WatcherChannel).TryUpdate(key, pValue, ttl)
}
type Watcher struct {
Namespaces sync.Map // map[string]*WatcherChild
}
func (w *Watcher) UseTarget(namespace string) *WatcherChild {
child, ok := w.Namespaces.Load(namespace)
if !ok {
child, _ = w.Namespaces.LoadOrStore(namespace, &WatcherChild{Namespace: namespace})
}
return child.(*WatcherChild)
}