-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
153 lines (138 loc) · 2.82 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
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
package toyqueue
import (
"errors"
"sync"
)
func (recs Records) recrem(total int64) (prelen int, prerem int64) {
for len(recs) > prelen && int64(len(recs[prelen])) <= total {
total -= int64(len(recs[prelen]))
prelen++
}
prerem = total
return
}
func (recs Records) WholeRecordPrefix(limit int64) (prefix Records, remainder int64) {
prelen, remainder := recs.recrem(limit)
prefix = recs[:prelen]
return
}
func (recs Records) ExactSuffix(total int64) (suffix Records) {
prelen, prerem := recs.recrem(total)
suffix = recs[prelen:]
if prerem != 0 { // damages the original, hence copy
edited := make(Records, 1, len(suffix))
edited[0] = suffix[0][prerem:]
suffix = append(edited, suffix[1:]...)
}
return
}
func (recs Records) TotalLen() (total int64) {
for _, r := range recs {
total += int64(len(r))
}
return
}
type RecordQueue struct {
recs Records
lock sync.Mutex
cond sync.Cond
Limit int
}
var ErrWouldBlock = errors.New("the queue is over capacity")
var ErrClosed = errors.New("queue is closed")
func (q *RecordQueue) Drain(recs Records) error {
q.lock.Lock()
was0 := len(q.recs) == 0
if len(q.recs)+len(recs) > q.Limit {
q.lock.Unlock()
if q.Limit == 0 {
return ErrClosed
}
return ErrWouldBlock
}
q.recs = append(q.recs, recs...)
if was0 && q.cond.L != nil {
q.cond.Broadcast()
}
q.lock.Unlock()
return nil
}
func (q *RecordQueue) Close() error {
q.Limit = 0
return nil
}
func (q *RecordQueue) Feed() (recs Records, err error) {
q.lock.Lock()
if len(q.recs) == 0 {
err = ErrWouldBlock
if q.Limit == 0 {
err = ErrClosed
}
q.lock.Unlock()
return
}
wasfull := len(q.recs) >= q.Limit
recs = q.recs
q.recs = q.recs[len(q.recs):]
if wasfull && q.cond.L != nil {
q.cond.Broadcast()
}
q.lock.Unlock()
return
}
func (q *RecordQueue) Blocking() FeedDrainCloser {
if q.cond.L == nil {
q.cond.L = &q.lock
}
return &blockingRecordQueue{q}
}
type blockingRecordQueue struct {
queue *RecordQueue
}
func (bq *blockingRecordQueue) Close() error {
return bq.queue.Close()
}
func (bq *blockingRecordQueue) Drain(recs Records) error {
q := bq.queue
q.lock.Lock()
for len(recs) > 0 {
was0 := len(q.recs) == 0
for q.Limit <= len(q.recs) {
if q.Limit == 0 {
q.lock.Unlock()
return ErrClosed
}
q.cond.Wait()
}
qcap := q.Limit - len(q.recs)
if qcap > len(recs) {
qcap = len(recs)
}
q.recs = append(q.recs, recs[:qcap]...)
recs = recs[qcap:]
if was0 {
q.cond.Broadcast()
}
}
q.lock.Unlock()
return nil
}
func (bq *blockingRecordQueue) Feed() (recs Records, err error) {
q := bq.queue
q.lock.Lock()
wasfull := len(q.recs) >= q.Limit
for len(q.recs) == 0 {
if q.Limit == 0 {
q.lock.Unlock()
return nil, ErrClosed
}
q.cond.Wait()
}
recs = q.recs
q.recs = q.recs[len(q.recs):]
if wasfull {
q.cond.Broadcast()
}
q.lock.Unlock()
return
}