-
Notifications
You must be signed in to change notification settings - Fork 0
/
timequeue.go
376 lines (303 loc) · 7.98 KB
/
timequeue.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package timequeue
import (
"container/list"
"errors"
"math"
"os"
"os/signal"
"reflect"
"runtime"
"sync"
"sync/atomic"
"syscall"
"time"
)
type item struct {
Value interface{}
Expire time.Time
}
// TimeQueue represents a time queue
type TimeQueue struct {
lock sync.RWMutex
queue *list.List
worker *semaphore
leave chan interface{}
persistent bool
persistence *persistence
stopTime time.Time
stopPersistence bool
osSignal chan os.Signal
}
// TravFunc is used for traversing time queue, the argument of TravFunc is Value stored in queue item
type TravFunc func(interface{})
func (tq *TimeQueue) service() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var releaseRLock bool
for {
tq.lock.RLock()
if releaseRLock {
releaseRLock = false
}
if tq.queue.Len() == 0 {
tq.lock.RUnlock()
releaseRLock = true
tq.worker.peek(consumption)
continue
}
tq.worker.processAvailableSignal()
if time.Now().Before(tq.queue.Front().Value.(*node).item.Expire) {
tq.lock.RUnlock()
releaseRLock = true
here:
select {
case *tq.worker.addr() = <-tq.worker.channel():
switch tq.worker.view() {
case reconsumption:
if time.Now().Before(tq.queue.Front().Value.(*node).item.Expire) {
continue
}
case stop, resume:
tq.worker.doAction()
goto here
}
case <-time.After(tq.queue.Front().Value.(*node).item.Expire.Sub(time.Now())):
tq.worker.processAvailableSignal()
}
}
if !releaseRLock {
tq.lock.RUnlock()
}
tq.lock.Lock()
tq.worker.processAvailableSignal()
for tmp, iter := (*node)(nil), tq.queue.Front(); iter != nil; iter = tq.queue.Front() {
tmp = iter.Value.(*node)
if tmp.item.Expire.After(time.Now()) {
break
}
if tq.leave != nil {
tq.leave <- tmp.item.Value
}
select {
case <-tq.osSignal:
return
default:
if tq.persistent || (!tq.stopTime.IsZero() && (tmp.item.Expire.Before(tq.stopTime) || tmp.item.Expire.Equal(tq.stopTime))) {
tq.persistence.expired <- tmp.length
} else if !tq.stopPersistence {
tq.persistence.worker.send(stop)
tq.stopPersistence = true
}
}
tmp = nil
tq.queue.Remove(iter)
}
tq.lock.Unlock()
}
}
// New returns a initialized time queue
func New() *TimeQueue {
tq := &TimeQueue{
queue: list.New(),
worker: newSemaphore(1),
osSignal: make(chan os.Signal, 1),
}
signal.Notify(tq.osSignal, syscall.SIGINT, syscall.SIGTERM)
go tq.service()
return tq
}
// Persist enable persistence for time queue
func (tq *TimeQueue) Persist(filename string, maxExpired int64, value interface{}, registry map[string]interface{}) (*TimeQueue, error) {
if tq.persistent {
return tq, errors.New("persistence already enabled")
}
return tq, tq.withPersistence(filename, maxExpired, value, registry)
}
// MustPersist must enable persistence for time queue, otherwise panics if there are any error occurred
func (tq *TimeQueue) MustPersist(filename string, maxExpired int64, value interface{}, registry map[string]interface{}) *TimeQueue {
if tq.persistent {
panic("persistence already enabled")
}
if err := tq.withPersistence(filename, maxExpired, value, registry); err != nil {
panic(err)
}
return tq
}
func (tq *TimeQueue) insertAndCalculateOffset(n *node, offset *int64) {
if front, back := tq.queue.Front(), tq.queue.Back(); front == nil {
tq.queue.PushFront(n)
tq.worker.send(consumption)
} else if math.Abs(float64(n.item.Expire.UnixNano()-front.Value.(*node).item.Expire.UnixNano())) <=
math.Abs(float64(n.item.Expire.UnixNano()-back.Value.(*node).item.Expire.UnixNano())) {
iter := front
for tmp := (*node)(nil); iter != nil; iter = iter.Next() {
tmp = iter.Value.(*node)
if n.item.Expire.Before(tmp.item.Expire) {
break
}
*offset += tmp.length
}
if iter != nil {
tq.queue.InsertBefore(n, iter)
if iter == front {
tq.worker.send(reconsumption)
}
} else {
tq.queue.PushBack(n)
}
} else {
iter := back
for tmp := (*node)(nil); iter != nil; iter = iter.Prev() {
tmp = iter.Value.(*node)
if n.item.Expire.After(tmp.item.Expire) {
break
}
*offset += tmp.length
}
*offset = atomic.LoadInt64(&tq.persistence.validLength) - *offset
if iter != nil {
tq.queue.InsertAfter(n, iter)
} else {
tq.queue.PushFront(n)
tq.worker.send(reconsumption)
}
}
}
func (tq *TimeQueue) insert(n *node) {
if front, back := tq.queue.Front(), tq.queue.Back(); front == nil {
tq.queue.PushFront(n)
tq.worker.send(consumption)
} else if math.Abs(float64(n.item.Expire.UnixNano()-front.Value.(*node).item.Expire.UnixNano())) <
math.Abs(float64(n.item.Expire.UnixNano()-back.Value.(*node).item.Expire.UnixNano())) {
iter := front
for iter != nil && n.item.Expire.After(iter.Value.(*node).item.Expire) {
iter = iter.Next()
}
if iter != nil {
tq.queue.InsertBefore(n, iter)
if iter == front {
tq.worker.send(reconsumption)
}
} else {
tq.queue.PushBack(n)
}
} else {
iter := back
for iter != nil && n.item.Expire.Before(iter.Value.(*node).item.Expire) {
iter = iter.Prev()
}
if iter != nil {
tq.queue.InsertAfter(n, iter)
} else {
tq.queue.PushFront(n)
tq.worker.send(reconsumption)
}
}
}
func (tq *TimeQueue) enqueue(n *node) {
if tq.persistent {
tq.persistence.encoder.in <- &n.item
block := &blockinfo{}
tq.insertAndCalculateOffset(n, &block.offset)
select {
case <-tq.osSignal:
return
default:
block.data = <-tq.persistence.encoder.out
n.length = int64(len(block.data))
tq.persistence.stream <- block
}
} else {
tq.insert(n)
}
}
// EnQueue enters time queue, stay in queue for duration delay then leave immediately, it will leave immediately if delay less or equal than 0.
func (tq *TimeQueue) EnQueue(value interface{}, delay time.Duration) {
expireTime := time.Now().Add(delay)
n := &node{item: item{value, expireTime}}
tq.lock.Lock()
if delay <= 0 {
if tq.leave != nil {
tq.leave <- value
}
tq.lock.Unlock()
return
}
if tq.persistent && reflect.TypeOf(value).Kind() == reflect.Func {
panic("unsupported persistent type")
}
tq.enqueue(n)
tq.lock.Unlock()
}
// Receive returns a received only channel, which can be used for receiving Value left from queue
func (tq *TimeQueue) Receive(buf int) <-chan interface{} {
tq.lock.Lock()
defer tq.lock.Unlock()
if tq.leave != nil {
return tq.leave
}
tq.leave = make(chan interface{}, buf)
return tq.leave
}
// Traverse returns a received only channel, which can be used to receive traversing result
func (tq *TimeQueue) Traverse() <-chan interface{} {
ch := make(chan interface{})
go func() {
tq.lock.RLock()
defer tq.lock.RUnlock()
defer close(ch)
for elem := tq.queue.Front(); elem != nil; elem = elem.Next() {
ch <- elem.Value.(*node).item.Value
}
}()
return ch
}
// TraverseF traverses time queue tq with function f
func (tq *TimeQueue) TraverseF(f TravFunc) {
tq.lock.RLock()
defer tq.lock.RUnlock()
for elem := tq.queue.Front(); elem != nil; elem = elem.Next() {
f(elem.Value.(*node).item.Value)
}
}
// SetMaxExpiredStorage sets maximum expired data in bytes
func (tq *TimeQueue) SetMaxExpiredStorage(maxExpired int64) {
tq.lock.RLock()
defer tq.lock.RUnlock()
if !tq.persistent {
panic("persistence not enabled")
}
tq.persistence.clean <- maxExpired
}
// Persistent reports whether persistence is enabled
func (tq *TimeQueue) Persistent() bool {
tq.lock.RLock()
defer tq.lock.RUnlock()
return tq.persistent
}
// Len returns the length of time queue
func (tq *TimeQueue) Len() int {
tq.lock.RLock()
defer tq.lock.RUnlock()
return tq.queue.Len()
}
// PersistOff turns persistence off
func (tq *TimeQueue) PersistOff() *TimeQueue {
tq.lock.RLock()
defer tq.lock.RUnlock()
if !tq.persistent {
return tq
}
tq.worker.send(stop)
return tq
}
// PersistOn turns persistence on
func (tq *TimeQueue) PersistOn() *TimeQueue {
tq.lock.RLock()
defer tq.lock.RUnlock()
if tq.persistent {
return tq
}
tq.worker.send(resume)
return tq
}