-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription.go
More file actions
181 lines (134 loc) · 2.77 KB
/
subscription.go
File metadata and controls
181 lines (134 loc) · 2.77 KB
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
package eventstore
import (
"fmt"
"sync"
"time"
"github.com/cockroachdb/pebble"
)
type StoreHandler func(*Event)
type Subscription struct {
store *Store
durableName string
lastSequence uint64
iterator *pebble.Iterator
cf *ColumnFamily
newTriggered chan struct{}
watchFn StoreHandler
isClosed bool
mutex sync.Mutex
}
type SubOpt func(s *Subscription)
func DurableName(durable string) SubOpt {
return func(s *Subscription) {
s.durableName = durable
}
}
func StartAtSequence(seq uint64) SubOpt {
return func(s *Subscription) {
s.lastSequence = seq
}
}
func NewSubscription(store *Store, cf *ColumnFamily, fn StoreHandler, opts ...SubOpt) *Subscription {
s := &Subscription{
store: store,
cf: cf,
durableName: "",
newTriggered: make(chan struct{}, 1),
isClosed: false,
watchFn: fn,
}
for _, o := range opts {
o(s)
}
// Load last sequence from store for durable name
if len(s.durableName) != 0 && s.lastSequence == 0 {
s.lastSequence, _ = store.GetDurableState(s.durableName)
}
return s
}
func (sub *Subscription) Close() {
close(sub.newTriggered)
sub.mutex.Lock()
sub.isClosed = true
sub.mutex.Unlock()
}
func (sub *Subscription) Watch() {
sub.pull()
for _ = range sub.newTriggered {
sub.pull()
}
// sub.iterator.Close()
}
func (sub *Subscription) Trigger() error {
if sub.isClosed {
return nil
}
select {
case sub.newTriggered <- struct{}{}:
default:
return nil
}
return nil
}
func (sub *Subscription) pull() {
sub.mutex.Lock()
defer sub.mutex.Unlock()
if sub.isClosed {
return
}
cur, _ := sub.cf.List([]byte(""), Uint64ToBytes(sub.lastSequence), &ListOptions{
WithoutRawPrefix: true,
})
for !sub.isClosed {
if cur.EOF() {
break
}
// Getting sequence number
key := cur.GetKey()
seq := BytesToUint64(key)
// If we get record which is the same with last seq, find next one
if seq == sub.lastSequence {
// Get next one
cur.Next()
continue
}
sub.lastSequence = seq
// Invoke data handler
value := cur.GetData()
sub.handle(seq, value)
// Get next one
cur.Next()
}
cur.Close()
}
func (sub *Subscription) handle(seq uint64, data []byte) {
event := eventPool.Get().(*Event)
event.Sequence = seq
event.Data = data
defer eventPool.Put(event)
defer event.reset()
sub.watchFn(event)
for {
if sub.isClosed {
return
}
select {
case <-event.completed:
sub.updateDurableOffset()
return
default:
}
}
}
func (sub *Subscription) updateDurableOffset() {
for !sub.isClosed {
// Update offset for this durable name
err := sub.store.UpdateDurableState(nil, sub.durableName, sub.lastSequence)
if err == nil {
return
}
fmt.Println(err)
// Retry in second
<-time.After(time.Second)
}
}