-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfifo.go
More file actions
46 lines (37 loc) · 691 Bytes
/
fifo.go
File metadata and controls
46 lines (37 loc) · 691 Bytes
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
package lasr
import "sync"
// fifo is for buffering received messages
type fifo struct {
data []*Message
sync.Mutex
}
func newFifo(size int) *fifo {
return &fifo{
data: make([]*Message, 0, size),
}
}
func (f *fifo) Pop() *Message {
msg := f.data[0]
f.data = append(f.data[0:0], f.data[1:]...)
return msg
}
func (f *fifo) Push(m *Message) {
if len(f.data) == cap(f.data) {
panic("push to full buffer")
}
f.data = append(f.data, m)
}
func (f *fifo) Len() int {
return len(f.data)
}
func (f *fifo) Cap() int {
return cap(f.data)
}
func (f *fifo) SetError(err error) {
for i := range f.data {
f.data[i].err = err
}
}
func (f *fifo) Drain() {
f.data = f.data[0:0]
}