-
Notifications
You must be signed in to change notification settings - Fork 111
/
queue.go
72 lines (67 loc) · 1.67 KB
/
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
package library
import (
"container/heap"
"github.com/nytlabs/streamtools/st/blocks" // blocks
"time"
)
// specify those channels we're going to use to communicate with streamtools
type Queue struct {
blocks.Block
queryPop chan chan interface{}
queryPeek chan chan interface{}
inPush chan interface{}
inPop chan interface{}
out chan interface{}
quit chan interface{}
}
// we need to build a simple factory so that streamtools can make new blocks of this kind
func NewQueue() blocks.BlockInterface {
return &Queue{}
}
// Setup is called once before running the block. We build up the channels and specify what kind of block this is.
func (b *Queue) Setup() {
b.Kind = "Queue"
b.Desc = "FIFO queue allowing push & pop on streams plus popping from a query"
b.inPush = b.InRoute("push")
b.inPop = b.InRoute("pop")
b.queryPop = b.QueryRoute("pop")
b.queryPeek = b.QueryRoute("peek")
b.quit = b.Quit()
b.out = b.Broadcast()
}
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *Queue) Run() {
pq := &PriorityQueue{}
heap.Init(pq)
for {
select {
case <-b.quit:
// quit the block
return
case msg := <-b.inPush:
queueMessage := &PQMessage{
val: msg,
t: time.Now(),
}
heap.Push(pq, queueMessage)
case <-b.inPop:
if len(*pq) == 0 {
continue
}
msg := heap.Pop(pq).(*PQMessage).val
b.out <- msg
case respChan := <-b.queryPop:
var msg interface{}
if len(*pq) > 0 {
msg = heap.Pop(pq).(*PQMessage).val
}
respChan <- msg
case respChan := <-b.queryPeek:
var msg interface{}
if len(*pq) > 0 {
msg = pq.Peek().(*PQMessage).val
}
respChan <- msg
}
}
}