generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
265 lines (216 loc) · 5.87 KB
/
stream.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package plex
import (
"context"
"io"
"sync"
)
// Interface enforcer
var _ Writer = (*writeStream)(nil)
var _ Reader = (*readStream)(nil)
// readStream wraps an io.ReadCloser and provides a set of methods for
// reading from the the underlying reader. It also bypasses the close method
// so that the stream can be closed but the reader can be peristed by the
// creator of the stream through a closure. The Multiplexer uses this feature
// to ensure it properly re-queues the stream when this stream is closed so
// that is it available to be used again.
type readStream struct {
io.ReadCloser
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
mu sync.Mutex
cleanup func()
}
// Read is a passthrough to the underlying io.Reader with the exception of
// a failure in the underlying io.Reader which causes it to no longer be viable
// in that case an appropriate error is returned.
//
// TODO: Define and export this error
func (s *readStream) Read(b []byte) (int, error) {
// TODO: this needs to adhere to the context so that it can
// be exited in the event the stream is closed due to an error
// and a new connection is established
n, err := s.ReadCloser.Read(b)
return n, err
}
// Recv provides a read-only channel for reading bytes from the underlying
// io.Reader. This is useful for situations where streaming data is desired
// rather than buffering through the use of the io.Reader interface. This
// method accepts a context as well as a buffer size. The buffer size is used
// to set the internal buffers for the returned channel as well as the buffer
// used to read data off the underlying io.Reader.
func (s *readStream) Recv(ctx context.Context, buffer int) (<-chan byte, error) {
ctx, _ = _ctx(ctx)
out := make(chan byte, buffer)
go func(out chan<- byte, r io.Reader, buffer int) {
defer func() {
_ = recover() // TODO: handle in the future?
}()
defer func() {
_ = s.Kill() // TODO: should this kill the underlying stream?
}()
defer close(out)
// Cannot have a buffer less than 1 when calling a reader
if buffer < 1 {
buffer = 1
}
// Create the buffer to read into
// Buffer doesn't need to be re-created
// on each read so this buffer is shared
// and the number of bytes read to the buffer
// is returned from the io.Reader based
// on the API defined in the go doc for io.Reader
b := make([]byte, buffer)
for {
select {
case <-ctx.Done():
return
default:
read, err := r.Read(b)
if err != nil {
// TODO: handle error
return
}
// Take each buffer value and push it to the stream
for _, v := range b[:read] {
select {
case <-ctx.Done():
return
case out <- v:
}
}
}
}
}(out, s, buffer)
return out, nil
}
func (s *readStream) Kill() (err error) {
defer func() {
err = recoverErr(err, recover())
}()
defer func() {
err = recoverErr(err, s.ReadCloser.Close())
}()
s.mu.Lock()
// Bypass cleanup since we are killing the stream
s.cleanup = nil
s.mu.Unlock()
err = s.Close()
return err
}
func (s *readStream) Close() (err error) {
defer func() {
err = recoverErr(err, recover())
}()
s.cancel()
<-s.ctx.Done()
s.mu.Lock()
defer s.mu.Unlock()
defer func() {
if s.cleanup == nil {
return
}
s.cleanup()
}()
s.wg.Wait()
return nil
}
// writeStream wraps an io.WriteCloser and provides a set of methods for
// writing from the the underlying writer. It also bypasses the close method
// so that the stream can be closed but the writer can be peristed by the
// creator of the stream through a closure. The Multiplexer uses this feature
// to ensure it properly re-queues the stream when this stream is closed so
// that is it available to be used again.
type writeStream struct {
io.WriteCloser
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
mu sync.Mutex
cleanup func()
}
func (s *writeStream) Write(b []byte) (int, error) {
// TODO: this needs to adhere to the context so that it can
// be exited in the event the stream is closed due to an error
// and a new connection is established
n, err := s.WriteCloser.Write(b)
return n, err
}
// Send provides a write-only channel for writing bytes to the underlying
// io.Writer. This is useful for situations where streaming data is desired
// rather than buffering through the use of the io.Writer interface. This
// method accepts a context as well as a buffer size. The buffer size is used
// to set the internal buffers for the returned channel as well as the buffer
// used to write data to the underlying io.Writer.
func (s *writeStream) Send(ctx context.Context, buffer int) (chan<- byte, error) {
ctx, _ = _ctx(ctx)
out := make(chan byte)
go func(out chan byte, w io.Writer) {
defer func() {
_ = recover() // TODO: handle in the future?
}()
defer func() {
_ = s.Kill()
}()
if buffer < 1 {
buffer = 1
}
buff := make([]byte, buffer)
index := 0
for {
select {
case <-ctx.Done():
return
case b, ok := <-out:
if ok {
buff[index] = b
index++
}
if index == buffer || !ok {
out := make([]byte, len(buff[:index]))
copy(out, buff[:index])
_, _ = w.Write(out)
// TODO: handle error
// TODO: handle n
buff = make([]byte, buffer)
index = 0
}
if !ok {
return
}
}
}
}(out, s)
return out, nil
}
func (s *writeStream) Kill() (err error) {
defer func() {
err = recoverErr(err, recover())
}()
defer func() {
err = recoverErr(err, s.WriteCloser.Close())
}()
s.mu.Lock()
// Bypass cleanup since we are killing the stream
s.cleanup = nil
s.mu.Unlock()
err = s.Close()
return err
}
func (s *writeStream) Close() (err error) {
defer func() {
err = recoverErr(err, recover())
}()
s.cancel()
<-s.ctx.Done()
s.mu.Lock()
defer s.mu.Unlock()
defer func() {
if s.cleanup == nil {
return
}
s.cleanup()
}()
s.wg.Wait()
return nil
}