-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.go
168 lines (132 loc) · 2.79 KB
/
observer.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 bolted
import (
"context"
"sync"
"github.com/draganm/bolted/dbpath"
)
type observer struct {
mu *sync.RWMutex
receivers map[int]*receiver
nextReceiverKey int
}
func (o *observer) broadcastChanges(changes ObservedChanges) {
o.mu.RLock()
defer o.mu.RUnlock()
for _, r := range o.receivers {
r.notify(changes)
}
}
type receiver struct {
m dbpath.Matcher
eventsChan chan<- ObservedChanges
incoming chan<- ObservedChanges
}
func (r *receiver) notify(changes ObservedChanges) {
matchingChanges := ObservedChanges{}
for _, ch := range changes {
if ch.Type == ChangeTypeDeleted || r.m.Matches(ch.Path) {
matchingChanges = matchingChanges.Update(ch.Path, ch.Type)
}
}
if len(matchingChanges) == 0 {
return
}
r.incoming <- matchingChanges
}
func newReceiver(m dbpath.Matcher) (*receiver, <-chan ObservedChanges) {
ch := make(chan ObservedChanges, 1)
ch <- ObservedChanges{}
incoming := make(chan ObservedChanges, 1)
go func() {
buffer := []ObservedChanges{}
for {
if len(buffer) == 0 {
ev, ok := <-incoming
if !ok {
// reading cancelled
close(ch)
return
}
select {
case ch <- ev:
// all good
default:
// ok, have to wait
buffer = append(buffer, ev)
}
continue
}
select {
case ch <- buffer[0]:
buffer = buffer[1:]
case ev, ok := <-incoming:
if !ok {
// reading cancelled
close(ch)
return
}
buffer = append(buffer, ev)
}
}
}()
return &receiver{
m: m,
eventsChan: ch,
incoming: incoming,
}, ch
}
func (r *receiver) close() {
close(r.incoming)
}
func newObserver() *observer {
return &observer{
mu: new(sync.RWMutex),
receivers: make(map[int]*receiver),
}
}
func (w *observer) observe(ctx context.Context, m dbpath.Matcher) <-chan ObservedChanges {
w.mu.Lock()
receiver, changesChan := newReceiver(m)
receiverKey := w.nextReceiverKey
w.receivers[receiverKey] = receiver
w.nextReceiverKey++
w.mu.Unlock()
go func() {
<-ctx.Done()
w.mu.Lock()
defer w.mu.Unlock()
delete(w.receivers, receiverKey)
receiver.close()
}()
return changesChan
}
type txObserver struct {
o *observer
changes []ObservedChange
}
func (o *observer) newWTxObserver() *txObserver {
return &txObserver{
o: o,
}
}
func (to *txObserver) delete(path dbpath.Path) {
to.changes = append(to.changes, ObservedChange{
Path: path,
Type: ChangeTypeDeleted,
})
}
func (to *txObserver) createMap(path dbpath.Path) {
to.changes = append(to.changes, ObservedChange{
Path: path,
Type: ChangeTypeMapCreated,
})
}
func (to *txObserver) put(path dbpath.Path) {
to.changes = append(to.changes, ObservedChange{
Path: path,
Type: ChangeTypeValueSet,
})
}
func (to *txObserver) broadcast() {
to.o.broadcastChanges(to.changes)
}