-
Notifications
You must be signed in to change notification settings - Fork 32
/
fifo_queue.go
261 lines (214 loc) · 7.15 KB
/
fifo_queue.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
package goconcurrentqueue
import (
"context"
"fmt"
"sync"
"time"
)
const (
WaitForNextElementChanCapacity = 1000
dequeueOrWaitForNextElementInvokeGapTime = 10
)
// FIFO (First In First Out) concurrent queue
type FIFO struct {
slice []interface{}
rwmutex sync.RWMutex
lockRWmutex sync.RWMutex
isLocked bool
// queue for watchers that will wait for next elements (if queue is empty at DequeueOrWaitForNextElement execution )
waitForNextElementChan chan chan interface{}
// queue to unlock consumers that were locked when queue was empty (during DequeueOrWaitForNextElement execution)
unlockDequeueOrWaitForNextElementChan chan struct{}
}
// NewFIFO returns a new FIFO concurrent queue
func NewFIFO() *FIFO {
ret := &FIFO{}
ret.initialize()
return ret
}
func (st *FIFO) initialize() {
st.slice = make([]interface{}, 0)
st.waitForNextElementChan = make(chan chan interface{}, WaitForNextElementChanCapacity)
st.unlockDequeueOrWaitForNextElementChan = make(chan struct{}, WaitForNextElementChanCapacity)
}
// Enqueue enqueues an element. Returns error if queue is locked.
func (st *FIFO) Enqueue(value interface{}) error {
if st.isLocked {
return NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked")
}
// let consumers (DequeueOrWaitForNextElement) know there is a new element
select {
case st.unlockDequeueOrWaitForNextElementChan <- struct{}{}:
default:
// message could not be sent
}
// check if there is a listener waiting for the next element (this element)
select {
case listener := <-st.waitForNextElementChan:
// send the element through the listener's channel instead of enqueue it
select {
case listener <- value:
default:
// enqueue if listener is not ready
// lock the object to enqueue the element into the slice
st.rwmutex.Lock()
// enqueue the element
st.slice = append(st.slice, value)
defer st.rwmutex.Unlock()
}
default:
// lock the object to enqueue the element into the slice
st.rwmutex.Lock()
// enqueue the element
st.slice = append(st.slice, value)
defer st.rwmutex.Unlock()
}
return nil
}
// Dequeue dequeues an element. Returns error if queue is locked or empty.
func (st *FIFO) Dequeue() (interface{}, error) {
if st.isLocked {
return nil, NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked")
}
st.rwmutex.Lock()
defer st.rwmutex.Unlock()
len := len(st.slice)
if len == 0 {
return nil, NewQueueError(QueueErrorCodeEmptyQueue, "empty queue")
}
elementToReturn := st.slice[0]
st.slice = st.slice[1:]
return elementToReturn, nil
}
// DequeueOrWaitForNextElement dequeues an element (if exist) or waits until the next element gets enqueued and returns it.
// Multiple calls to DequeueOrWaitForNextElement() would enqueue multiple "listeners" for future enqueued elements.
func (st *FIFO) DequeueOrWaitForNextElement() (interface{}, error) {
return st.DequeueOrWaitForNextElementContext(context.Background())
}
// DequeueOrWaitForNextElementContext dequeues an element (if exist) or waits until the next element gets enqueued and returns it.
// Multiple calls to DequeueOrWaitForNextElementContext() would enqueue multiple "listeners" for future enqueued elements.
// When the passed context expires this function exits and returns the context' error
func (st *FIFO) DequeueOrWaitForNextElementContext(ctx context.Context) (interface{}, error) {
for {
if st.isLocked {
return nil, NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked")
}
// get the slice's len
st.rwmutex.Lock()
length := len(st.slice)
st.rwmutex.Unlock()
if length == 0 {
// channel to wait for next enqueued element
waitChan := make(chan interface{})
select {
// enqueue a watcher into the watchForNextElementChannel to wait for the next element
case st.waitForNextElementChan <- waitChan:
// n
for {
// re-checks every i milliseconds (top: 10 times) ... the following verifies if an item was enqueued
// around the same time DequeueOrWaitForNextElementContext was invoked, meaning the waitChan wasn't yet sent over
// st.waitForNextElementChan
for i := 0; i < dequeueOrWaitForNextElementInvokeGapTime; i++ {
select {
case <-ctx.Done():
return nil, ctx.Err()
case dequeuedItem := <-waitChan:
return dequeuedItem, nil
case <-time.After(time.Millisecond * time.Duration(i)):
if dequeuedItem, err := st.Dequeue(); err == nil {
return dequeuedItem, nil
}
}
}
// return the next enqueued element, if any
select {
// new enqueued element, no need to keep waiting
case <-st.unlockDequeueOrWaitForNextElementChan:
// check if we got a new element just after we got <-st.unlockDequeueOrWaitForNextElementChan
select {
case item := <-waitChan:
return item, nil
default:
}
// go back to: for loop
continue
case item := <-waitChan:
return item, nil
case <-ctx.Done():
return nil, ctx.Err()
}
// n
}
default:
// too many watchers (waitForNextElementChanCapacity) enqueued waiting for next elements
return nil, NewQueueError(QueueErrorCodeEmptyQueue, "empty queue and can't wait for next element because there are too many DequeueOrWaitForNextElement() waiting")
}
}
st.rwmutex.Lock()
// verify that at least 1 item resides on the queue
if len(st.slice) == 0 {
st.rwmutex.Unlock()
continue
}
elementToReturn := st.slice[0]
st.slice = st.slice[1:]
st.rwmutex.Unlock()
return elementToReturn, nil
}
}
// Get returns an element's value and keeps the element at the queue
func (st *FIFO) Get(index int) (interface{}, error) {
if st.isLocked {
return nil, NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked")
}
st.rwmutex.RLock()
defer st.rwmutex.RUnlock()
if len(st.slice) <= index {
return nil, NewQueueError(QueueErrorCodeIndexOutOfBounds, fmt.Sprintf("index out of bounds: %v", index))
}
return st.slice[index], nil
}
// Remove removes an element from the queue
func (st *FIFO) Remove(index int) error {
if st.isLocked {
return NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked")
}
st.rwmutex.Lock()
defer st.rwmutex.Unlock()
if len(st.slice) <= index {
return NewQueueError(QueueErrorCodeIndexOutOfBounds, fmt.Sprintf("index out of bounds: %v", index))
}
// remove the element
st.slice = append(st.slice[:index], st.slice[index+1:]...)
return nil
}
// GetLen returns the number of enqueued elements
func (st *FIFO) GetLen() int {
st.rwmutex.RLock()
defer st.rwmutex.RUnlock()
return len(st.slice)
}
// GetCap returns the queue's capacity
func (st *FIFO) GetCap() int {
st.rwmutex.RLock()
defer st.rwmutex.RUnlock()
return cap(st.slice)
}
// Lock // Locks the queue. No enqueue/dequeue operations will be allowed after this point.
func (st *FIFO) Lock() {
st.lockRWmutex.Lock()
defer st.lockRWmutex.Unlock()
st.isLocked = true
}
// Unlock unlocks the queue
func (st *FIFO) Unlock() {
st.lockRWmutex.Lock()
defer st.lockRWmutex.Unlock()
st.isLocked = false
}
// IsLocked returns true whether the queue is locked
func (st *FIFO) IsLocked() bool {
st.lockRWmutex.RLock()
defer st.lockRWmutex.RUnlock()
return st.isLocked
}