-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopipe.go
179 lines (162 loc) · 4.33 KB
/
gopipe.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
package gopipe
import (
"log"
"time"
)
/*
Pipe is a single component that processes items. Pipes can be composed to form a pipeline
*/
type Pipe interface {
Process(in interface{}) (out interface{}, next bool)
}
/*
Pipeline connects multiple pipes in order. The head chan receives incoming items
and tail chan send out items that the pipeline has finished processing.
*/
type Pipeline struct {
bufferSize int // Chan buffer size
head chan interface{} // Chan representing the head of the pipeline
tail chan interface{} // Chan representing the tail of the pipeline
DebugMode bool // Option to log pipeline state transitions, false by default
}
/*
Enqueue takes an item one at a time and adds it to the start of the pipeline.
Use AttachSource to attach a chan of incoming items to the pipeline.
If the pipeline is blocked, this is going to be a blocking operation
*/
func (p *Pipeline) Enqueue(item interface{}) {
p.head <- item
}
/*
Dequeue will block till an item is available and then dequeue from the pipeline.
*/
func (p *Pipeline) Dequeue() interface{} {
return <-p.tail
}
/*
DequeueTimeout will block till an item is available and then dequeue from the pipeline.
*/
func (p *Pipeline) DequeueTimeout(t time.Duration) interface{} {
timeout := make(chan bool, 1)
go func() {
time.Sleep(t)
timeout <- true
}()
select {
case item := <-p.tail:
// a read from tail has occurred
return item
case <-timeout:
// the read has timed out
return nil
}
}
/*
Close makes sure that the pipeline accepts no further messages.
If the go routine/method writing to the pipeline is still Enqueuing, it will
cause a panic - can't write to a closed channel
*/
func (p *Pipeline) Close() {
close(p.head)
}
// String prints a helpful debug state of the pipeline
func (p *Pipeline) String() {
log.Printf("BufferSize: %d Head: %v Tail: %v DebugMode: %t", p.bufferSize, p.head,
p.tail, p.DebugMode)
}
/*
AddPipe attaches a pipe to the end of the pipeline.
This will immediately start routing items to this newly attached pipe
*/
func (p *Pipeline) AddPipe(pipe Pipe) *Pipeline {
oldTail := p.tail
newTail := make(chan interface{}, p.bufferSize)
startPipe(oldTail, newTail, pipe)
p.tail = newTail
return p
}
/*
AddJunction creates a new Junction to this pipeline and immediately start routing
to that junction
*/
func (p *Pipeline) AddJunction(j Junction) {
j.route(p.tail)
}
/*
AttachSource accepts the source channel as the entry point to the pipeline
*/
func (p *Pipeline) AttachSource(source chan interface{}) {
p.debug("Attaching source channel to pipeline")
go func() {
for item := range source {
p.head <- item
}
p.debug("Pipeline source closed. Closing rest of the pipeline")
p.Close()
}()
}
/*
AttachSink takes a terminating channel and dequeues the messages
from the pipeline onto that channel.
*/
func (p *Pipeline) AttachSink(out chan interface{}) {
p.debug("Attaching sink to pipeline")
go func() {
for item := range p.tail {
out <- item
}
p.debug("Shutting down pipeline Sink")
close(out)
}()
}
/*
debug Prints log statements if debugLog is true
*/
func (p *Pipeline) debug(values ...string) {
if p.DebugMode {
log.Println(values)
}
}
/*
NewBufferedPipeline creates a Pipeline with channel buffers set to the given size.
This is useful in increasing processing speed. NewPipeline should mostly always
be tried first.
*/
func NewBufferedPipeline(s int, pipes ...Pipe) *Pipeline {
if len(pipes) == 0 {
// Without pipes, just join head and tail
h := make(chan interface{}, s)
return &Pipeline{
bufferSize: s,
head: h,
tail: h,
}
}
tail := make(chan interface{}, s)
globalHead := make(chan interface{}, s)
head := globalHead
for _, pipe := range pipes {
tail = make(chan interface{}, s)
startPipe(head, tail, pipe)
head = tail
}
return &Pipeline{bufferSize: s, head: globalHead, tail: tail}
}
/*
NewPipeline takes multiple pipes in-order and connects them to form a pipeline.
Enqueue and Dequeue methods are used to attach source/sink to the pipeline.
If debugLog is true, logs state transitions to stdout.
*/
func NewPipeline(pipes ...Pipe) *Pipeline {
return NewBufferedPipeline(0, pipes...)
}
func startPipe(head, tail chan interface{}, pipe Pipe) {
go func() {
for in := range head {
if out, next := pipe.Process(in); next {
tail <- out
}
}
close(tail)
}()
}