Debian Codegraph @

internal/queue/queue.go

raw
1 package queue
2
3 import (
4 "sync"
5 )
6
7 // Queue is an unbounded queue of []byte.
8 // The queue is goroutine safe.
9 // Inspired by http://blog.dubbelboer.com/2015/04/25/go-faster-queue.html (MIT)
10 type Queue interface {
11 // Add an []byte to the back of the queue
12 // will return false if the queue is closed.
13 // In that case the []byte is dropped.
14 Add([]byte) bool
15
16 // Remove will remove a []byte from the queue.
17 // If false is returned, it either means 1) there were no items on the queue
18 // or 2) the queue is closed.
19 Remove() ([]byte, bool)
20
21 // Close the queue and discard all entries in the queue
22 // all goroutines in wait() will return
23 Close()
24
25 // CloseRemaining will close the queue and return all entries in the queue.
26 // All goroutines in wait() will return
27 CloseRemaining() [][]byte
28
29 // Closed returns true if the queue has been closed
30 // The call cannot guarantee that the queue hasn't been
31 // closed while the function returns, so only "true" has a definite meaning.
32 Closed() bool
33
34 // Wait for a []byte to be added or queue to be closed.
35 // If there is items on the queue the first will
36 // be returned immediately.
37 // Will return "", false if the queue is closed.
38 // Otherwise the return value of "remove" is returned.
39 Wait() bool
40
41 // Cap returns the capacity (without allocations).
42 Cap() int
43
44 // Len returns the current length of the queue.
45 Len() int
46
47 // Size returns the current size of the queue in bytes.
48 Size() int
49 }
50
51 type byteQueue struct {
52 mu sync.RWMutex
53 cond *sync.Cond
54 nodes [][]byte
55 head int
56 tail int
57 cnt int
58 size int
59 closed bool
60 initCap int
61 }
62
63 var initialCapacity = 2
64
65 // New ByteQueue returns a new []byte queue with initial capacity.
66 func New() Queue {
67 sq := &byteQueue{
68 initCap: initialCapacity,
69 nodes: make([][]byte, initialCapacity),
70 }
71 sq.cond = sync.NewCond(&sq.mu)
72 return sq
73 }
74
75 // Write mutex must be held when calling
76 func (q *byteQueue) resize(n int) {
77 nodes := make([][]byte, n)
78 if q.head < q.tail {
79 copy(nodes, q.nodes[q.head:q.tail])
80 } else {
81 copy(nodes, q.nodes[q.head:])
82 copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.tail])
83 }
84
85 q.tail = q.cnt % n
86 q.head = 0
87 q.nodes = nodes
88 }
89
90 // Add a []byte to the back of the queue
91 // will return false if the queue is closed.
92 // In that case the []byte is dropped.
93 func (q *byteQueue) Add(i []byte) bool {
94 q.mu.Lock()
95 if q.closed {
96 q.mu.Unlock()
97 return false
98 }
99 if q.cnt == len(q.nodes) {
100 // Also tested a grow rate of 1.5, see: http://stackoverflow.com/questions/2269063/buffer-growth-strategy
101 // In Go this resulted in a higher memory usage.
102 q.resize(q.cnt * 2)
103 }
104 q.nodes[q.tail] = i
105 q.tail = (q.tail + 1) % len(q.nodes)
106 q.size += len(i)
107 q.cnt++
108 q.cond.Signal()
109 q.mu.Unlock()
110 return true
111 }
112
113 // Close the queue and discard all entries in the queue
114 // all goroutines in wait() will return
115 func (q *byteQueue) Close() {
116 q.mu.Lock()
117 defer q.mu.Unlock()
118 q.closed = true
119 q.cnt = 0
120 q.nodes = nil
121 q.size = 0
122 q.cond.Broadcast()
123 }
124
125 // CloseRemaining will close the queue and return all entries in the queue.
126 // All goroutines in wait() will return.
127 func (q *byteQueue) CloseRemaining() [][]byte {
128 q.mu.Lock()
129 defer q.mu.Unlock()
130 if q.closed {
131 return [][]byte{}
132 }
133 rem := make([][]byte, 0, q.cnt)
134 for q.cnt > 0 {
135 i := q.nodes[q.head]
136 q.head = (q.head + 1) % len(q.nodes)
137 q.cnt--
138 rem = append(rem, i)
139 }
140 q.closed = true
141 q.cnt = 0
142 q.nodes = nil
143 q.size = 0
144 q.cond.Broadcast()
145 return rem
146 }
147
148 // Closed returns true if the queue has been closed
149 // The call cannot guarantee that the queue hasn't been
150 // closed while the function returns, so only "true" has a definite meaning.
151 func (q *byteQueue) Closed() bool {
152 q.mu.RLock()
153 c := q.closed
154 q.mu.RUnlock()
155 return c
156 }
157
158 // Wait for a message to be added.
159 // If there are items on the queue will return immediately.
160 // Will return false if the queue is closed.
161 // Otherwise returns true.
162 func (q *byteQueue) Wait() bool {
163 q.mu.Lock()
164 if q.closed {
165 q.mu.Unlock()
166 return false
167 }
168 if q.cnt != 0 {
169 q.mu.Unlock()
170 return true
171 }
172 q.cond.Wait()
173 q.mu.Unlock()
174 return true
175 }
176
177 // Remove will remove a []byte from the queue.
178 // If false is returned, it either means 1) there were no items on the queue
179 // or 2) the queue is closed.
180 func (q *byteQueue) Remove() ([]byte, bool) {
181 q.mu.Lock()
182 if q.cnt == 0 {
183 q.mu.Unlock()
184 return nil, false
185 }
186 i := q.nodes[q.head]
187 q.head = (q.head + 1) % len(q.nodes)
188 q.cnt--
189 q.size -= len(i)
190
191 if n := len(q.nodes) / 2; n >= q.initCap && q.cnt <= n {
192 q.resize(n)
193 }
194
195 q.mu.Unlock()
196 return i, true
197 }
198
199 // Cap returns the capacity (without allocations)
200 func (q *byteQueue) Cap() int {
201 q.mu.RLock()
202 c := cap(q.nodes)
203 q.mu.RUnlock()
204 return c
205 }
206
207 // Len returns the current length of the queue.
208 func (q *byteQueue) Len() int {
209 q.mu.RLock()
210 l := q.cnt
211 q.mu.RUnlock()
212 return l
213 }
214
215 // Size returns the current size of the queue.
216 func (q *byteQueue) Size() int {
217 q.mu.RLock()
218 s := q.size
219 q.mu.RUnlock()
220 return s
221 }