-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.go
288 lines (241 loc) · 5.05 KB
/
watch.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package riverdb
import (
"context"
"errors"
"fmt"
"github.com/246859/containers/queues"
"slices"
"sync"
)
var (
ErrWatcherClosed = errors.New("watcher is closed")
ErrInvalidEvent = errors.New("invalid event")
ErrWatcherDisabled = errors.New("event watcher disabled")
)
type EventType uint
func (e EventType) String() string {
switch e {
case PutEvent:
return "PutEvent"
case DelEvent:
return "DelEvent"
case RollbackEvent:
return "RollbackEvent"
case MergeEvent:
return "MergeEvent"
case BackupEvent:
return "BackupEvent"
case RecoverEvent:
return "RecoverEvent"
default:
return fmt.Sprintf("%d", e)
}
}
// Event represents a push event
type Event struct {
Type EventType
Value any
}
const (
PutEvent EventType = 1 + iota
DelEvent
RollbackEvent
MergeEvent
BackupEvent
RecoverEvent
)
func validEvent(et EventType) bool {
ets := []EventType{
PutEvent,
DelEvent,
RollbackEvent,
MergeEvent,
BackupEvent,
RecoverEvent,
}
return slices.Contains(ets, et)
}
// Watcher returns a new event watcher with the given event type, if events is empty, it will apply db.Option
func (db *DB) Watcher(name string, events ...EventType) (*Watcher, error) {
if db.flag.Check(closed) {
return nil, ErrDBClosed
}
if db.option.WatchSize == 0 {
return nil, ErrWatcherDisabled
}
for _, event := range events {
if !validEvent(event) {
return nil, ErrInvalidEvent
}
}
w := db.watcher.newWatcher(name, events...)
return w, nil
}
func newWatcherPool(ctx context.Context, queueSize int, events ...EventType) (*watcherPool, error) {
for _, event := range events {
if !validEvent(event) {
return nil, ErrInvalidEvent
}
}
w := &watcherPool{
ctx: ctx,
expectedEvent: events,
pool: make([]*Watcher, 0, 20),
queueSize: queueSize,
eventQueue: queues.NewArrayQueue[*Event](queueSize),
}
w.chanSize = queueSize / 10
if queueSize < 100 {
w.chanSize = queueSize / 5
} else if queueSize <= 0 {
w.chanSize = 10
}
return w, nil
}
type watcherPool struct {
ctx context.Context
mu sync.Mutex
pool []*Watcher
expectedEvent []EventType
eventQueue *queues.ArrayQueue[*Event]
queueSize int
chanSize int
done chan struct{}
}
func (w *watcherPool) newWatcher(name string, ets ...EventType) *Watcher {
if len(ets) == 0 {
ets = w.expectedEvent
}
wa := &Watcher{name: name}
wa.watch = w
// chan buffer size a little greater than eventsize is to prevent to block watch goroutine if chan is full
wa.ch = make(chan *Event, w.chanSize+5)
wa.expectedEvent = ets
w.pool = append(w.pool, wa)
return wa
}
func (w *watcherPool) expected(e EventType) bool {
return expectedEvents(w.expectedEvent, e)
}
func (w *watcherPool) pop() *Event {
w.mu.Lock()
defer w.mu.Unlock()
e, has := w.eventQueue.Pop()
if !has {
return nil
}
return e
}
func (w *watcherPool) push(e *Event) {
w.mu.Lock()
size := w.eventQueue.Size()
w.mu.Unlock()
if size >= w.queueSize {
w.pop()
}
w.mu.Lock()
w.eventQueue.Push(e)
w.mu.Unlock()
}
// watch the events in the event queue, and notify the watcher in pool
// it must be running in another goroutine, and it will return when db closed
func (w *watcherPool) watch() {
var wg sync.WaitGroup
defer func() {
wg.Wait()
w.Close()
}()
for {
select {
case <-w.ctx.Done():
return
default:
if len(w.pool) == 0 {
continue
}
event := w.pop()
if event == nil {
continue
}
// notify watcher in pool
for _, wa := range w.pool {
wg.Add(1)
go func(wa *Watcher) {
defer wg.Done()
if w.eventQueue == nil {
return
}
if wa.closed || !wa.expected(event.Type) {
return
}
for len(wa.ch) >= w.chanSize {
<-wa.ch
}
wa.ch <- event
}(wa)
}
}
}
}
// Close closes all the live watchers in pool and discard self
func (w *watcherPool) Close() {
w.mu.Lock()
defer w.mu.Unlock()
for _, wa := range w.pool {
if wa.closed {
continue
}
close(wa.ch)
}
w.pool = nil
w.eventQueue = nil
}
// only use for Watcher to call
func (w *watcherPool) closeCh(ch chan *Event) {
for i, wa := range w.pool {
if wa.closed {
return
}
if wa.ch == ch {
close(wa.ch)
w.pool = slices.Delete(w.pool, i, i+1)
wa.ch = nil
break
}
}
}
// Watcher
// a watcher should be closed after it used up all, or it will be closed when db closed at last
type Watcher struct {
watch *watcherPool
ch chan *Event
expectedEvent []EventType
closed bool
name string
}
func (w *Watcher) Name() string {
return w.name
}
func (w *Watcher) Listen() (<-chan *Event, error) {
if w.closed {
return nil, ErrWatcherClosed
}
return w.ch, nil
}
func (w *Watcher) expected(e EventType) bool {
return expectedEvents(w.expectedEvent, e)
}
// Close closes the watcher.
func (w *Watcher) Close() error {
w.watch.mu.Lock()
defer w.watch.mu.Unlock()
if w.closed {
return ErrWatcherClosed
}
w.watch.closeCh(w.ch)
w.closed = true
return nil
}
func expectedEvents(events []EventType, e EventType) bool {
return slices.Contains(events, e)
}