-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfifo_disk_queue.go
183 lines (175 loc) · 4.19 KB
/
fifo_disk_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package queue
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
)
func NewFifoDiskQueue(file string) (Queue, error) {
var err error
ctx, cancel := context.WithCancel(context.Background())
queue := FifoDiskQueue{
ctx: ctx,
cancel: cancel,
}
queue.writeFile, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return nil, err
}
queue.readFile, err = os.OpenFile(file, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
stat, err := queue.readFile.Stat()
if err != nil {
return nil, err
}
if stat.Size() == 0 {
return &queue, nil
}
_, err = queue.writeFile.Seek(-4, io.SeekEnd)
if err != nil {
return nil, err
}
buf := make([]byte, 4)
_, err = queue.writeFile.Read(buf)
if err != nil {
return nil, err
}
var length int32
err = binary.Read(bytes.NewBuffer(buf), binary.BigEndian, &length)
if err != nil {
return nil, err
}
offset, err := queue.writeFile.Seek(int64(-4-length), io.SeekCurrent)
if err != nil {
return nil, err
}
buf = make([]byte, length)
_, err = queue.writeFile.Read(buf)
if err != nil {
return nil, err
}
bufs := strings.Split(string(buf), ",")
if len(bufs) != 2 {
return nil, fmt.Errorf("%s 格式异常", string(buf))
}
indexString := bufs[0]
offsetString := bufs[1]
queue.index, err = strconv.Atoi(indexString)
if err != nil {
return nil, err
}
queue.offset, err = strconv.Atoi(offsetString)
if err != nil {
return nil, err
}
err = queue.writeFile.Truncate(offset)
if err != nil {
return nil, err
}
_, err = queue.writeFile.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
_, err = queue.readFile.Seek(int64(queue.offset), io.SeekStart)
if err != nil {
return nil, err
}
return &queue, nil
}
var _ Queue = (*FifoDiskQueue)(nil)
type FifoDiskQueue struct {
index int
offset int
readFile *os.File
writeFile *os.File
lock sync.Mutex
ctx context.Context
cancel context.CancelFunc
}
func (q *FifoDiskQueue) Get(ctx context.Context) ([]byte, error) {
select {
case <-q.ctx.Done():
return nil, ErrQueueClosed
default:
}
q.lock.Lock()
defer q.lock.Unlock()
if q.index <= 0 {
return nil, ErrQueueEmpty
}
buf := make([]byte, 4)
_, err := q.readFile.Read(buf)
if err != nil {
return nil, err
}
var length int32
err = binary.Read(bytes.NewBuffer(buf), binary.BigEndian, &length)
if err != nil {
return nil, err
}
buf = make([]byte, length)
_, err = q.readFile.Read(buf)
if err != nil {
return nil, err
}
q.index--
q.offset += int(length) + 4
return buf, nil
}
func (q *FifoDiskQueue) Put(ctx context.Context, data []byte) error {
select {
case <-q.ctx.Done():
return ErrQueueClosed
default:
}
q.lock.Lock()
defer q.lock.Unlock()
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.BigEndian, int32(len(data)))
_, err := q.writeFile.Write(bytes.Join([][]byte{buf.Bytes(), data}, []byte("")))
if err != nil {
return err
}
q.index++
return nil
}
func (q *FifoDiskQueue) Close() error {
select {
case <-q.ctx.Done():
return nil
default:
}
q.lock.Lock()
defer q.lock.Unlock()
q.cancel()
defer func() {
q.readFile.Close()
q.writeFile.Close()
}()
if q.index < 1 {
return q.writeFile.Truncate(0)
}
offset, err := q.writeFile.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
err = q.writeFile.Truncate(offset)
if err != nil {
return err
}
data := fmt.Sprintf("%d,%d", q.index, q.offset)
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.BigEndian, int32(len(data)))
_, err = q.writeFile.Write(bytes.Join([][]byte{[]byte(data), buf.Bytes()}, []byte("")))
return err
}
func (q *FifoDiskQueue) Len() int {
return q.index
}