-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimerqueue.go
203 lines (169 loc) · 4.01 KB
/
timerqueue.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
// Package timerqueue provides a timer queue for a list of items that should be processed
// after a fixed duration of time from when they are added to the queue.
// All operations are safe for concurrent use.
package timerqueue
import (
"sync"
"time"
)
// Queue holds a list of elements. When a new element is added to the
// queue, the queue callback will be called with the element after
// the set queue duration.
type Queue struct {
first, last *element
m map[interface{}]*element
mu sync.Mutex
duration time.Duration
cb func(v interface{})
}
type element struct {
v interface{}
next, prev *element
time time.Time
}
// New creates a new timer Queue
func New(callback func(interface{}), duration time.Duration) *Queue {
return &Queue{
m: make(map[interface{}]*element),
duration: duration,
cb: callback,
}
}
// Add adds a new element to the timer Queue, starting a timer
// for the fixed duration for the queue. Once the duration
// has passed, the queue callback will be called.
func (q *Queue) Add(v interface{}) {
q.mu.Lock()
defer q.mu.Unlock()
// Ensure it is not a duplicate
if _, ok := q.m[v]; ok {
panic("Value already in queue")
}
el := &element{v: v, time: time.Now().Add(q.duration)}
q.m[v] = el
q.push(el)
if el == q.first {
go q.timer(el, el.time)
}
}
// Len returns the number of elements in the queue
func (q *Queue) Len() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.m)
}
// Clear removes all elements from the queue.
// Returns a slice of the elements cleared from the queue.
func (q *Queue) Clear() []interface{} {
el, l := q.clear()
elems := make([]interface{}, l)
for i := 0; el != nil; i++ {
elems[i] = el.v
el = el.next
}
return elems
}
// Flush calls the callback for each element in the queue.
// Any new element added while flushing, will not be called.
func (q *Queue) Flush() {
el, _ := q.clear()
for el != nil {
q.cb(el.v)
el = el.next
}
}
// Remove removes an element from the queue.
// Returns false if the element was not in the queue, otherwise true.
func (q *Queue) Remove(v interface{}) bool {
q.mu.Lock()
defer q.mu.Unlock()
el, ok := q.m[v]
if !ok {
return false
}
first := q.first
delete(q.m, v)
q.remove(el)
// If the element was first, we need to start a new timer
if first == el && q.first != nil {
go q.timer(q.first, q.first.time)
}
return true
}
// Reset sets the time of the element callback back to full duration.
// Returns false if the the element was not in the queue, otherwise true.
func (q *Queue) Reset(v interface{}) bool {
q.mu.Lock()
defer q.mu.Unlock()
el, ok := q.m[v]
if !ok {
return false
}
el.time = time.Now().Add(q.duration)
first := q.first
q.remove(el)
q.push(el)
// If the element was first, we need to start a new timer
if first == el {
go q.timer(q.first, q.first.time)
}
return true
}
func (q *Queue) clear() (*element, int) {
q.mu.Lock()
defer q.mu.Unlock()
first := q.first
q.first = nil
q.last = nil
l := len(q.m)
q.m = make(map[interface{}]*element)
return first, l
}
func (q *Queue) remove(el *element) {
if q.first == el {
q.first = el.next
} else {
el.prev.next = el.next
}
if q.last == el {
q.last = el.prev
} else {
el.next.prev = el.prev
}
}
func (q *Queue) push(el *element) {
last := q.last
if last != nil {
last.next = el
el.prev = last
} else {
q.first = el
el.prev = nil
}
el.next = nil
q.last = el
}
func (q *Queue) timer(el *element, t time.Time) {
var v interface{}
for {
time.Sleep(t.Sub(time.Now()))
q.mu.Lock()
// Check if the first element has changed
if el != q.first || t != el.time {
q.mu.Unlock()
break
}
v = el.v
q.remove(el)
delete(q.m, v)
el = q.first
if el == nil {
q.mu.Unlock()
q.cb(v)
break
}
t = el.time
q.mu.Unlock()
q.cb(v)
}
}