delayqueue.go
3.99 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
182
183
package kafkatimer
import (
"container/heap"
"sync"
"sync/atomic"
"time"
)
type item struct {
Value interface{}
Priority int64
Index int
}
// this is a priority queue as implemented by a min heap
// ie. the 0th element is the *lowest* value
type priorityQueue []*item
func newPriorityQueue(capacity int) priorityQueue {
return make(priorityQueue, 0, capacity)
}
func (pq priorityQueue) Len() int {
return len(pq)
}
func (pq priorityQueue) Less(i, j int) bool {
return pq[i].Priority < pq[j].Priority
}
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].Index = i
pq[j].Index = j
}
func (pq *priorityQueue) Push(x interface{}) {
n := len(*pq)
c := cap(*pq)
if n+1 > c {
npq := make(priorityQueue, n, c*2)
copy(npq, *pq)
*pq = npq
}
*pq = (*pq)[0 : n+1]
item := x.(*item)
item.Index = n
(*pq)[n] = item
}
func (pq *priorityQueue) Pop() interface{} {
n := len(*pq)
c := cap(*pq)
if n < (c/2) && c > 25 {
npq := make(priorityQueue, n, c/2)
copy(npq, *pq)
*pq = npq
}
item := (*pq)[n-1]
item.Index = -1
*pq = (*pq)[0 : n-1]
return item
}
func (pq *priorityQueue) PeekAndShift(max int64) (*item, int64) {
if pq.Len() == 0 {
return nil, 0
}
item := (*pq)[0]
if item.Priority > max {
return nil, item.Priority - max
}
heap.Remove(pq, 0)
return item, 0
}
// The end of PriorityQueue implementation.
// DelayQueue is an unbounded blocking queue of *Delayed* elements, in which
// an element can only be taken when its delay has expired. The head of the
// queue is the *Delayed* element whose delay expired furthest in the past.
type DelayQueue struct {
C chan interface{}
mu sync.Mutex
pq priorityQueue
// Similar to the sleeping state of runtime.timers.
sleeping int32
wakeupC chan struct{}
}
// New creates an instance of delayQueue with the specified size.
func New(size int) *DelayQueue {
return &DelayQueue{
C: make(chan interface{}),
pq: newPriorityQueue(size),
wakeupC: make(chan struct{}),
}
}
// Offer inserts the element into the current queue.
func (dq *DelayQueue) Offer(elem interface{}, expiration int64) {
item := &item{Value: elem, Priority: expiration}
dq.mu.Lock()
heap.Push(&dq.pq, item)
index := item.Index
dq.mu.Unlock()
if index == 0 {
// A new item with the earliest expiration is added.
if atomic.CompareAndSwapInt32(&dq.sleeping, 1, 0) {
dq.wakeupC <- struct{}{}
}
}
}
// Poll starts an infinite loop, in which it continually waits for an element
// to expire and then send the expired element to the channel C.
func (dq *DelayQueue) Poll(exitC chan struct{}, nowF func() int64) {
for {
now := nowF()
dq.mu.Lock()
item, delta := dq.pq.PeekAndShift(now)
if item == nil {
// No items left or at least one item is pending.
// We must ensure the atomicity of the whole operation, which is
// composed of the above PeekAndShift and the following StoreInt32,
// to avoid possible race conditions between Offer and Poll.
atomic.StoreInt32(&dq.sleeping, 1)
}
dq.mu.Unlock()
if item == nil {
if delta == 0 {
// No items left.
select {
case <-dq.wakeupC:
// Wait until a new item is added.
continue
case <-exitC:
goto exit
}
} else if delta > 0 {
// At least one item is pending.
select {
case <-dq.wakeupC:
// A new item with an "earlier" expiration than the current "earliest" one is added.
continue
case <-time.After(time.Duration(delta) * time.Millisecond):
// The current "earliest" item expires.
// Reset the sleeping state since there's no need to receive from wakeupC.
if atomic.SwapInt32(&dq.sleeping, 0) == 0 {
// A caller of Offer() is being blocked on sending to wakeupC,
// drain wakeupC to unblock the caller.
<-dq.wakeupC
}
continue
case <-exitC:
goto exit
}
}
}
select {
case dq.C <- item.Value:
// The expired element has been sent out successfully.
case <-exitC:
goto exit
}
}
exit:
// Reset the states
atomic.StoreInt32(&dq.sleeping, 0)
}