-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.go
164 lines (135 loc) · 2.92 KB
/
unit.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
package pipeline
import (
"context"
"runtime"
"sync"
"time"
)
type NextUnit[I any] interface {
Input() chan<- I
Start() error
Stop(context.Context) error
}
type Unit[I, O any] struct {
workers int
sharedStates []State
OnExecute func(I) (O, error)
OnError func(error, I)
StartedAt time.Time
EndedAt time.Time
inputChannel chan I
outputChannel chan O
doneChannel chan struct{}
nextUnit NextUnit[O]
}
func NewUnit[I, O any](opts ...Option[I, O]) *Unit[I, O] {
u := &Unit[I, O]{
workers: runtime.NumCPU(),
inputChannel: make(chan I),
doneChannel: make(chan struct{}, 1),
}
for _, opt := range opts {
opt(u)
}
u.sharedStates = make([]State, u.workers)
u.outputChannel = make(chan O, u.workers)
return u
}
// Input Channel for receiving input data.
func (u *Unit[I, O]) Input() chan<- I {
return u.inputChannel
}
// Output Channel for receiving output data.
// Throw panic if the unit has next unit set.
func (u *Unit[I, O]) Output() <-chan O {
if u.HasNextUnit() {
panic("Next unit has been defined. You cannot use this output.")
}
return u.outputChannel
}
func (u *Unit[I, O]) Start() error {
if u.OnExecute == nil {
return ErrExecuteMethodNotSet
}
if !u.EndedAt.IsZero() {
return ErrUnitDoneAlready
}
if !u.StartedAt.IsZero() {
return ErrUnitStartedAlready
}
u.StartedAt = time.Now()
var wg sync.WaitGroup
wg.Add(u.workers)
for i := 0; i < u.workers; i++ {
go func() {
defer wg.Done()
for task := range u.inputChannel {
u.execute(i, task)
}
}()
}
go func() {
wg.Wait()
if !u.HasNextUnit() {
close(u.outputChannel)
}
u.doneChannel <- struct{}{}
u.EndedAt = time.Now()
}()
if u.HasNextUnit() {
return u.nextUnit.Start()
}
return nil
}
func (u *Unit[I, O]) Stop(ctx context.Context) error {
if u.StartedAt.IsZero() {
return ErrUnitNotStarted
}
if !u.EndedAt.IsZero() {
return ErrUnitStoppedAlready
}
close(u.inputChannel)
select {
case <-ctx.Done():
return ctx.Err()
case <-u.doneChannel:
if u.HasNextUnit() {
return u.nextUnit.Stop(ctx)
}
return nil
}
}
func (u *Unit[I, O]) GetState() *State {
state := &State{}
for i := 0; i < len(u.sharedStates); i++ {
state.Total.Add(u.sharedStates[i].Total.Load())
state.Done.Add(u.sharedStates[i].Done.Load())
state.Errors.Add(u.sharedStates[i].Errors.Load())
}
return state
}
func (u *Unit[I, O]) execute(worker int, i I) {
u.sharedStates[worker].Total.Add(1)
if o, err := u.OnExecute(i); err != nil {
u.sharedStates[worker].Errors.Add(1)
if u.OnError != nil {
u.OnError(err, i)
}
} else {
u.sharedStates[worker].Done.Add(1)
if u.HasNextUnit() {
u.nextUnit.Input() <- o
} else {
u.outputChannel <- o
}
}
}
// SetNextUnit
// Deletes the output channel and redirects a result to the next unit input
func (u *Unit[I, O]) SetNextUnit(unit NextUnit[O]) {
u.nextUnit = unit
u.outputChannel = nil
}
func (u *Unit[I, O]) HasNextUnit() bool {
return u.nextUnit != nil
}