-
Notifications
You must be signed in to change notification settings - Fork 0
/
streams.go
243 lines (215 loc) · 5.77 KB
/
streams.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package asyncigo
import (
"bytes"
"context"
"errors"
"io"
"slices"
"syscall"
)
// AsyncStream is a byte stream that can be read from and written to asynchronously.
type AsyncStream struct {
file AsyncReadWriteCloser
buffer []byte
writeLock Mutex
}
// NewAsyncStream constructs a new [AsyncStream].
func NewAsyncStream(file AsyncReadWriteCloser) *AsyncStream {
return &AsyncStream{
file: file,
}
}
// Close closes the stream.
func (a *AsyncStream) Close() error {
return a.file.Close()
}
func (a *AsyncStream) read(ctx context.Context, maxBytes int) (n int, err error) {
if len(a.buffer) >= maxBytes {
return maxBytes, nil
}
if cap(a.buffer) < maxBytes {
a.buffer = slices.Grow(a.buffer, maxBytes)
}
for {
readN, err := a.file.Read(a.buffer[len(a.buffer):maxBytes])
if readN > 0 {
a.buffer = a.buffer[:len(a.buffer)+readN]
}
if errors.Is(err, syscall.EAGAIN) || errors.Is(err, syscall.EWOULDBLOCK) {
if err = a.file.WaitForReady(ctx); err == nil {
continue
}
}
return len(a.buffer), err
}
}
// Write writes the given data to the stream.
// The returned [Awaitable] can be awaited to be sure that all data has been written before continuing.
func (a *AsyncStream) Write(ctx context.Context, data []byte) Awaitable[int] {
return SpawnTask(ctx, func(ctx context.Context) (int, error) {
// prevent chunks from being interleaved if multiple tasks are writing at the same time
if err := a.writeLock.Lock(ctx); err != nil {
return 0, err
}
defer a.writeLock.Unlock()
var bytesWritten int
for {
n, err := a.file.Write(data)
if n > 0 {
bytesWritten += n
data = data[n:]
}
if errors.Is(err, syscall.EAGAIN) || errors.Is(err, syscall.EWOULDBLOCK) {
err = a.file.WaitForReady(ctx)
}
if err != nil || len(data) == 0 {
return bytesWritten, err
}
}
})
}
func (a *AsyncStream) consumeInto(buf []byte) (n int) {
n = copy(buf, a.buffer)
copy(a.buffer, a.buffer[n:])
a.buffer = a.buffer[:len(a.buffer)-n]
return n
}
func (a *AsyncStream) consume(maxBytes int) []byte {
buf := make([]byte, min(maxBytes, len(a.buffer)))
n := a.consumeInto(buf)
return buf[:n]
}
func (a *AsyncStream) consumeAll() []byte {
buf := slices.Clone(a.buffer)
a.buffer = a.buffer[:0]
return buf
}
// Stream returns an AsyncIterable that yields the next chunk of data as soon as it is available.
// The chunks will be no larger than the given buffer size.
func (a *AsyncStream) Stream(ctx context.Context, bufSize int) AsyncIterable[[]byte] {
return AsyncIter(func(yield func([]byte) error) error {
for {
n, err := a.read(ctx, bufSize)
if n > 0 {
if err := yield(a.consumeAll()); err != nil {
return err
}
}
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
}
})
}
// Chunks returns an AsyncIterable that iterates over the stream in fixed-size chunks of data.
func (a *AsyncStream) Chunks(ctx context.Context, chunkSize int) AsyncIterable[[]byte] {
return AsyncIter(func(yield func([]byte) error) error {
for {
var err error
for len(a.buffer) < chunkSize && err == nil {
_, err = a.read(ctx, chunkSize)
}
if len(a.buffer) > 0 {
if err := yield(a.consume(chunkSize)); err != nil {
return err
}
}
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
}
})
}
func (a *AsyncStream) yieldLines(yield func([]byte) error, data []byte) error {
start := 0
for i, b := range data {
if b == '\n' || i == len(data)-1 {
if err := yield(data[start : i+1]); err != nil {
return err
}
start = i + 1
}
}
return nil
}
// Lines returns an AsyncIterable that iterates over all lines in the stream.
// The newline character will be included with each line.
func (a *AsyncStream) Lines(ctx context.Context) AsyncIterable[[]byte] {
return AsyncIter(func(yield func([]byte) error) error {
bufSize := 1024
scanned := 0
for {
_, err := a.read(ctx, bufSize)
if errors.Is(err, io.EOF) {
return a.yieldLines(yield, a.consumeAll())
} else if err != nil {
return err
}
for i := len(a.buffer) - 1; i >= scanned; i-- {
if a.buffer[i] == '\n' {
if err := a.yieldLines(yield, a.consume(i+1)); err != nil {
return err
}
break
}
}
scanned = len(a.buffer)
if len(a.buffer) >= bufSize {
bufSize *= 2
}
}
})
}
// ReadLine returns all data until a newline is encountered, including the newline.
func (a *AsyncStream) ReadLine(ctx context.Context) ([]byte, error) {
return a.ReadUntil(ctx, '\n')
}
// ReadUntil returns all data until the given character is encountered, including the character.
func (a *AsyncStream) ReadUntil(ctx context.Context, character byte) ([]byte, error) {
for i, b := range a.buffer {
if b == character {
return a.consume(i + 1), nil
}
}
bufSize := 1024
for {
n, err := a.read(ctx, bufSize)
for i := len(a.buffer) - n; i < len(a.buffer); i++ {
if a.buffer[i] == character {
return a.consume(i + 1), nil
}
}
if errors.Is(err, io.EOF) && len(a.buffer) > 0 {
return a.consumeAll(), nil
} else if err != nil {
return nil, err
}
if len(a.buffer) >= bufSize {
bufSize *= 2
}
}
}
// ReadChunk reads a single fixed-size chunk of data from the stream.
func (a *AsyncStream) ReadChunk(ctx context.Context, chunkSize int) ([]byte, error) {
var err error
for len(a.buffer) < chunkSize && err == nil {
_, err = a.read(ctx, chunkSize)
}
if err == nil || errors.Is(err, io.EOF) && len(a.buffer) > 0 {
return a.consume(chunkSize), nil
}
return nil, err
}
// ReadAll reads until the end of the stream and returns all read data.
func (a *AsyncStream) ReadAll(ctx context.Context) ([]byte, error) {
var buf bytes.Buffer
var err error
for chunk := range a.Stream(ctx, 1024).UntilErr(&err) {
buf.Write(chunk)
}
return buf.Bytes(), err
}