-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocessor.go
143 lines (119 loc) · 3.18 KB
/
processor.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
package jpipe
import (
"sync"
"github.com/junitechnology/jpipe/options"
)
type processor[T any, R any] func(value T) (outputValue R, send bool)
func (processor processor[T, R]) PooledWorker(opts ...options.PooledWorkerOption) worker[T, R] {
concurrent := getOptionOrDefault(opts, Concurrent(1))
ordered := getOption[options.PooledWorkerOption, options.Ordered](opts)
if concurrent.Concurrency == 1 {
return processor.singleLoopWorker()
} else if ordered == nil {
return processor.singleLoopWorker().Pooled(concurrent)
}
return processor.orderedPooledWorker(concurrent.Concurrency, ordered.OrderBufferSize)
}
func (processor processor[T, R]) singleLoopWorker() worker[T, R] {
return func(node workerNode[T, R]) {
node.LoopInput(0, func(value T) bool {
if output, send := processor(value); send {
return node.Send(output)
}
return true
})
}
}
func (processor processor[T, R]) orderedPooledWorker(concurrency int, orderBufferSize int) worker[T, R] {
return func(node workerNode[T, R]) {
internalInput := make(chan orderedValue[T])
orderingBuffer := NewOrderingBuffer(node, concurrency, orderBufferSize)
var wg sync.WaitGroup
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer func() {
wg.Done()
node.HandlePanic()
}()
loopOverChannel(node, internalInput, func(value orderedValue[T]) bool {
output, send := processor(*value.value)
return orderingBuffer.Send(orderedValue[R]{value: &output, idx: value.idx, skip: !send})
})
}()
}
orderingBuffer.Start()
idx := int64(0)
node.LoopInput(0, func(value T) bool {
internalInput <- orderedValue[T]{value: &value, idx: idx}
idx++
return true
})
close(internalInput)
wg.Wait()
orderingBuffer.Stop()
<-orderingBuffer.Done()
}
}
type orderedValue[T any] struct {
value *T
idx int64
skip bool
}
type orderingBuffer[T any, R any] struct {
node workerNode[T, R]
input chan orderedValue[R]
feedback chan struct{}
done chan struct{}
}
func NewOrderingBuffer[T any, R any](node workerNode[T, R], concurrency int, bufferSize int) *orderingBuffer[T, R] {
return &orderingBuffer[T, R]{
node: node,
input: make(chan orderedValue[R], bufferSize+concurrency),
feedback: make(chan struct{}, bufferSize),
done: make(chan struct{}),
}
}
func (b *orderingBuffer[T, R]) Send(value orderedValue[R]) bool {
b.input <- value
select {
case <-b.feedback:
return true
case <-b.node.QuitSignal():
return false
}
}
func (b *orderingBuffer[T, R]) Start() {
go func() {
for i := 0; i < cap(b.feedback); i++ {
b.feedback <- struct{}{}
}
buffer := map[int64]orderedValue[R]{}
currentIndex := int64(0)
loopOverChannel(b.node, b.input, func(value orderedValue[R]) bool {
buffer[value.idx] = value
for {
value, ok := buffer[currentIndex]
if !ok {
return true
}
delete(buffer, currentIndex)
if !value.skip {
sent := b.node.Send(*value.value)
if !sent {
return false
}
}
currentIndex++
b.feedback <- struct{}{}
}
})
close(b.done)
}()
}
func (b *orderingBuffer[T, R]) Stop() {
close(b.input)
}
func (b *orderingBuffer[T, R]) Done() <-chan struct{} {
return b.done
}