-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.go
252 lines (214 loc) · 5.94 KB
/
node.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
package jpipe
import (
"fmt"
"runtime/debug"
"sync"
"github.com/junitechnology/jpipe/options"
)
type pipelineNode interface {
Start()
Done() <-chan struct{}
Children() []pipelineNode
IsSource() bool
IsSink() bool
}
type node[T any, R any] struct {
lock sync.Mutex
nodeType string
pipeline *Pipeline
inputs []*Channel[T]
outputs []*Channel[R]
outputWriters []chan<- R
subscriptions []chan struct{}
allUnsubscribed chan struct{}
worker worker[T, R]
quitSignal chan struct{}
doneSignal chan struct{}
}
type workerNode[T any, R any] interface {
Inputs() []*Channel[T]
LoopInput(i int, function func(value T) bool)
Send(value R) bool
QuitSignal() <-chan struct{}
HandlePanic()
}
func newPipelineNode[T any, R any](
nodeType string,
pipeline *Pipeline,
inputs []*Channel[T],
numOutputs int,
worker worker[T, R],
sharedOutput bool,
opts ...options.NodeOption) (pipelineNode, []*Channel[R]) {
buffered := getOptionOrDefault(opts, Buffered(0))
node := &node[T, R]{
nodeType: nodeType,
pipeline: pipeline,
inputs: inputs,
outputs: make([]*Channel[R], numOutputs),
subscriptions: make([]chan struct{}, numOutputs),
allUnsubscribed: make(chan struct{}),
worker: worker,
quitSignal: make(chan struct{}),
doneSignal: make(chan struct{}),
}
for _, input := range inputs {
input.setToNode(node)
}
sharedOutputChannel := make(chan R, buffered.Size)
if sharedOutput {
node.outputWriters = []chan<- R{sharedOutputChannel}
} else {
node.outputWriters = make([]chan<- R, numOutputs)
}
for i := 0; i < numOutputs; i++ {
var goChannel chan R
if sharedOutput {
goChannel = sharedOutputChannel
} else {
goChannel = make(chan R, buffered.Size)
node.outputWriters[i] = goChannel
}
n := i // avoid capturing the loop var i in the unsubscriber closure
node.outputs[i] = newChannel(pipeline, goChannel, func() { node.unsubscribe(n) })
node.subscriptions[i] = make(chan struct{})
}
pipeline.addNode(node)
return node, node.outputs
}
func newLinearPipelineNode[T any, R any](nodeType string, input *Channel[T], worker worker[T, R], opts ...options.NodeOption) (pipelineNode, *Channel[R]) {
node, outputs := newPipelineNode(nodeType, input.getPipeline(), []*Channel[T]{input}, 1, worker, false, opts...)
return node, outputs[0]
}
func newSourcePipelineNode[R any](nodeType string, pipeline *Pipeline, worker worker[any, R], opts ...options.NodeOption) (pipelineNode, *Channel[R]) {
node, outputs := newPipelineNode(nodeType, pipeline, []*Channel[any]{}, 1, worker, false, opts...)
return node, outputs[0]
}
func newSinkPipelineNode[T any](nodeType string, input *Channel[T], worker worker[T, any], opts ...options.NodeOption) pipelineNode {
node, _ := newPipelineNode(nodeType, input.getPipeline(), []*Channel[T]{input}, 0, worker, false, opts...)
return node
}
func (node *node[T, R]) Start() {
go func() {
defer func() {
for i := range node.outputWriters {
close(node.outputWriters[i])
}
for i := range node.inputs {
node.inputs[i].unsubscribe()
}
close(node.doneSignal)
}()
go func() {
select {
case <-node.pipeline.Done():
case <-node.allUnsubscribed:
for _, input := range node.inputs {
input.unsubscribe() // eagerly unsubscribe from inputs here
}
}
close(node.quitSignal)
}()
defer node.HandlePanic()
node.worker(node)
}()
}
func (node *node[T, R]) QuitSignal() <-chan struct{} {
return node.quitSignal
}
func (node *node[T, R]) Done() <-chan struct{} {
return node.doneSignal
}
func (node *node[T, R]) Children() []pipelineNode {
children := make([]pipelineNode, len(node.outputs))
for i := range node.outputs {
children[i] = node.outputs[i].getToNode()
}
return children
}
func (node *node[T, R]) IsSource() bool {
return len(node.inputs) == 0
}
func (node *node[T, R]) IsSink() bool {
return len(node.outputs) == 0
}
func (node *node[T, R]) Inputs() []*Channel[T] {
return node.inputs
}
func (node *node[T, R]) LoopInput(i int, function func(value T) bool) {
loopOverChannel[T, T, R](node, node.inputs[i].getChannel(), function)
}
func (node *node[T, R]) HandlePanic() {
if r := recover(); r != nil {
node.pipeline.Cancel(fmt.Errorf("%v: %s", r, debug.Stack()))
}
}
func (node *node[T, R]) Send(value R) bool {
// handle shared output case
if len(node.outputWriters) == 1 && len(node.outputs) > 1 {
select {
case <-node.quitSignal:
return false
default:
select {
case <-node.quitSignal:
return false
case <-node.allUnsubscribed:
return false
case node.outputWriters[0] <- value:
return true
}
}
}
success := false
for i := range node.outputs {
select {
case <-node.quitSignal:
return false // the nested select gives priority to the quit signal, so we always exit early if needed
default:
select {
case <-node.quitSignal:
return false
case node.outputWriters[i] <- value:
success = true // we return true if we sent to at least one subscriber. If we don't it means there's no active subscriber left.
case <-node.subscriptions[i]: // do nothing if subscription is canceled
}
}
}
return success
}
func (node *node[T, R]) unsubscribe(n int) {
node.lock.Lock()
defer node.lock.Unlock()
select {
case <-node.subscriptions[n]:
return // for idempotency
default:
close(node.subscriptions[n])
}
for _, s := range node.subscriptions {
select {
case <-s:
default: // if at least one subscription is still open, we don't have to close allUnsubscribed
return
}
}
close(node.allUnsubscribed)
}
func loopOverChannel[C any, T any, R any](node workerNode[T, R], channel <-chan C, function func(value C) bool) {
for {
select { // the nested select gives priority to the quit signal, so we always exit early if needed
case <-node.QuitSignal():
return
default:
select {
case <-node.QuitSignal():
return
case value, open := <-channel:
if !open || !function(value) {
return
}
}
}
}
}