-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq.go
53 lines (49 loc) · 832 Bytes
/
q.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
package priority
type queue struct {
out chan<- interface{}
in <-chan interface{}
feed <-chan interface{}
closer func()
}
func newQueue(out chan<- interface{}, in <-chan interface{}, closer func()) *queue {
return &queue{
out: out,
in: in,
closer: closer,
}
}
func (q *queue) start(feed <-chan interface{}) {
if feed == nil {
q.closer = nil
}
q.feed = feed
go q.manage()
}
func (q *queue) manage() {
for {
select {
case val, open := <-q.in:
if !open && q.feed == nil {
close(q.out)
return
}
q.out <- val
default:
select {
case val, open := <-q.in:
if !open && q.feed == nil {
close(q.out)
return
}
q.out <- val
case val, open := <-q.feed:
if !open {
q.feed = nil
q.closer()
continue
}
q.out <- val
}
}
}
}