Repository navigation
Expand file tree
/
Copy pathstream.go
More file actions
173 lines (144 loc) · 3.67 KB
/
Copy pathstream.go
File metadata and controls
173 lines (144 loc) · 3.67 KB
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
package fusion
import (
"bufio"
"context"
"encoding/binary"
"errors"
"io"
"sync"
)
var _ Stream = (*LineStream)(nil)
// Stream implementation is the source of data in a pipeline.
type Stream interface {
// Out should return a channel to which it independently writes the data
// stream to. Stream is responsible for closing the returned channel once
// the data is exhausted or when the stream worker exits. All goroutines
// spawned by the stream must exit when the given context is cancelled.
Out(ctx context.Context) (<-chan Msg, error)
}
// StreamFn implements a source using a Go function value.
type StreamFn func(ctx context.Context) (*Msg, error)
// Out launches a goroutine that continuously calls the wrapped function and
// writes the return message to the channel. Stops when ctx is cancelled or
// the function returns an error.
func (sf StreamFn) Out(ctx context.Context) (<-chan Msg, error) {
stream := make(chan Msg)
go func() {
defer close(stream)
for {
msg, err := sf(ctx)
if err != nil {
return
}
select {
case <-ctx.Done():
return
case stream <- *msg:
}
}
}()
return stream, nil
}
// LineStream implements a Stream using io.Reader. This implementation scans
// the reader line-by-line and streams each line as a message. If offset is
// set, 'offset' number of lines are read and skipped. If Size is set, only
// 'size' number of lines are read after which the source will return EOF.
type LineStream struct {
From io.Reader // From is the reader to use.
Offset int // Offset to start at.
Size int // Number of lines (from offset) to stream.
Buffer int // Stream channel buffer size.
// normal streaming states.
curOffset int
eofReached bool
reader *bufio.Reader
messages chan Msg
err error
// buffer for maintaining messages that got nAcked.
mu sync.Mutex
nAcked []*Msg
}
// Out sets up the source channel and sets up goroutines for writing to it.
func (rd *LineStream) Out(ctx context.Context) (<-chan Msg, error) {
if rd.From == nil {
return nil, errors.New("field From must be set")
}
rd.reader = bufio.NewReader(rd.From)
rd.messages = make(chan Msg, rd.Buffer)
go rd.stream(ctx)
return rd.messages, nil
}
// Err returns the error that caused the source to end.
func (rd *LineStream) Err() error { return rd.err }
func (rd *LineStream) stream(ctx context.Context) {
defer close(rd.messages)
for {
msg, err := rd.readOne()
if err != nil {
if err == io.EOF {
return
}
rd.err = err
break
}
if rd.curOffset <= rd.Offset {
continue
}
select {
case <-ctx.Done():
return
case rd.messages <- *msg:
}
}
}
func (rd *LineStream) readOne() (*Msg, error) {
if msg := rd.pop(); msg != nil {
return msg, nil
} else if rd.eofReached {
return nil, io.EOF
}
if rd.Size > 0 && rd.curOffset >= rd.Size+rd.Offset {
return nil, io.EOF
}
msg, err := rd.readLine()
if err != nil {
return nil, err
}
msg.Ack = func(err error) {
if err == nil || err == Fail || err == Skip {
return
}
rd.mu.Lock()
defer rd.mu.Unlock()
rd.nAcked = append(rd.nAcked, msg)
}
return msg, nil
}
func (rd *LineStream) pop() *Msg {
rd.mu.Lock()
defer rd.mu.Unlock()
if len(rd.nAcked) == 0 {
return nil
}
msg := rd.nAcked[0]
rd.nAcked = rd.nAcked[1:]
return msg
}
func (rd *LineStream) readLine() (*Msg, error) {
line, err := rd.reader.ReadBytes('\n')
if err != nil && err != io.EOF {
return nil, err
} else if err == io.EOF {
if string(line) == "" {
return nil, err
}
rd.eofReached = true
}
rd.curOffset++
var key [8]byte
binary.LittleEndian.PutUint64(key[:], uint64(rd.curOffset+rd.Offset-1))
return &Msg{
Key: key[:],
Val: line,
}, nil
}