/
stream.go
346 lines (289 loc) · 6.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2019-present Open Networking Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stream
import (
"container/list"
"sync"
)
// ReadStream is a state machine read stream
type ReadStream interface {
// Receive receives the next result
Receive() (Result, bool)
}
// WriteStream is a state machine write stream
type WriteStream interface {
// Send sends an output on the stream
Send(out Result)
// Result sends a result on the stream
Result(value interface{}, err error)
// Value sends a value on the stream
Value(value interface{})
// Error sends an error on the stream
Error(err error)
// Close closes the stream
Close()
}
// Stream is a read/write stream
type Stream interface {
ReadStream
WriteStream
}
// NewUnaryStream returns a new read/write stream that expects one result
func NewUnaryStream() Stream {
return &unaryStream{
cond: sync.NewCond(&sync.Mutex{}),
}
}
// unaryStream is a stream that expects one result
type unaryStream struct {
result *Result
closed bool
cond *sync.Cond
}
func (s *unaryStream) Receive() (Result, bool) {
s.cond.L.Lock()
defer s.cond.L.Unlock()
if s.result == nil {
s.cond.Wait()
}
result := s.result
s.result = nil
return *result, true
}
func (s *unaryStream) Send(result Result) {
s.cond.L.Lock()
defer s.cond.L.Unlock()
if s.closed {
panic("stream closed")
}
s.result = &result
s.closed = true
s.cond.Signal()
}
func (s *unaryStream) Result(value interface{}, err error) {
s.Send(Result{
Value: value,
Error: err,
})
}
func (s *unaryStream) Value(value interface{}) {
s.Send(Result{
Value: value,
})
}
func (s *unaryStream) Error(err error) {
s.Send(Result{
Error: err,
})
}
func (s *unaryStream) Close() {
s.cond.L.Lock()
defer s.cond.L.Unlock()
s.closed = true
}
// NewBufferedStream returns a new buffered read/write stream
func NewBufferedStream() Stream {
return &bufferedStream{
buffer: list.New(),
cond: sync.NewCond(&sync.Mutex{}),
}
}
// bufferedStream is a buffered read/write stream
type bufferedStream struct {
buffer *list.List
closed bool
cond *sync.Cond
}
func (s *bufferedStream) Receive() (Result, bool) {
s.cond.L.Lock()
defer s.cond.L.Unlock()
for s.buffer.Len() == 0 {
if s.closed {
return Result{}, false
}
s.cond.Wait()
}
result := s.buffer.Front().Value.(Result)
s.buffer.Remove(s.buffer.Front())
return result, true
}
func (s *bufferedStream) Send(result Result) {
s.cond.L.Lock()
defer s.cond.L.Unlock()
s.buffer.PushBack(result)
s.cond.Signal()
}
func (s *bufferedStream) Result(value interface{}, err error) {
s.Send(Result{
Value: value,
Error: err,
})
}
func (s *bufferedStream) Value(value interface{}) {
s.Send(Result{
Value: value,
})
}
func (s *bufferedStream) Error(err error) {
s.Send(Result{
Error: err,
})
}
func (s *bufferedStream) Close() {
s.cond.L.Lock()
defer s.cond.L.Unlock()
s.closed = true
}
// NewChannelStream returns a new channel-based stream
func NewChannelStream(ch chan<- Result) WriteStream {
return &channelStream{
ch: ch,
}
}
// channelStream is a channel-based stream
type channelStream struct {
ch chan<- Result
closed bool
}
func (s *channelStream) Send(result Result) {
s.ch <- result
}
func (s *channelStream) Result(value interface{}, err error) {
s.Send(Result{
Value: value,
Error: err,
})
}
func (s *channelStream) Value(value interface{}) {
s.Result(value, nil)
}
func (s *channelStream) Error(err error) {
s.Result(nil, err)
}
func (s *channelStream) Close() {
if !s.closed {
close(s.ch)
s.closed = true
}
}
// NewNilStream returns a disconnected stream
func NewNilStream() WriteStream {
return &nilStream{}
}
// nilStream is a stream that does not send messages
type nilStream struct{}
func (s *nilStream) Send(out Result) {
}
func (s *nilStream) Result(value interface{}, err error) {
}
func (s *nilStream) Value(value interface{}) {
}
func (s *nilStream) Error(err error) {
}
func (s *nilStream) Close() {
}
// NewEncodingStream returns a new encoding stream
func NewEncodingStream(stream WriteStream, encoder func(interface{}, error) (interface{}, error)) WriteStream {
return &transcodingStream{
stream: stream,
transcoder: encoder,
}
}
// NewDecodingStream returns a new decoding stream
func NewDecodingStream(stream WriteStream, encoder func(interface{}, error) (interface{}, error)) WriteStream {
return &transcodingStream{
stream: stream,
transcoder: encoder,
}
}
// transcodingStream is a stream that encodes output
type transcodingStream struct {
stream WriteStream
transcoder func(interface{}, error) (interface{}, error)
}
func (s *transcodingStream) Send(result Result) {
if result.Failed() {
s.stream.Send(result)
} else {
s.Value(result.Value)
}
}
func (s *transcodingStream) Result(value interface{}, err error) {
bytes, err := s.transcoder(value, err)
if err != nil {
s.stream.Error(err)
} else {
s.stream.Value(bytes)
}
}
func (s *transcodingStream) Value(value interface{}) {
bytes, err := s.transcoder(value, nil)
if err != nil {
s.stream.Error(err)
} else {
s.stream.Value(bytes)
}
}
func (s *transcodingStream) Error(err error) {
bytes, err := s.transcoder(nil, err)
if err != nil {
s.stream.Error(err)
} else {
s.stream.Value(bytes)
}
}
func (s *transcodingStream) Close() {
s.stream.Close()
}
// NewCloserStream returns a new stream that runs a function on close
func NewCloserStream(stream WriteStream, f func(WriteStream)) WriteStream {
return &closerStream{
stream: stream,
closer: f,
}
}
// closerStream is a stream that runs a function on close
type closerStream struct {
stream WriteStream
closer func(WriteStream)
}
func (s *closerStream) Send(result Result) {
s.stream.Send(result)
}
func (s *closerStream) Result(value interface{}, err error) {
s.stream.Result(value, err)
}
func (s *closerStream) Value(value interface{}) {
s.stream.Value(value)
}
func (s *closerStream) Error(err error) {
s.stream.Error(err)
}
func (s *closerStream) Close() {
s.closer(s)
s.stream.Close()
}
// Result is a stream result
type Result struct {
Value interface{}
Error error
}
// Failed returns a boolean indicating whether the operation failed
func (r Result) Failed() bool {
return r.Error != nil
}
// Succeeded returns a boolean indicating whether the operation was successful
func (r Result) Succeeded() bool {
return !r.Failed()
}