-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
193 lines (168 loc) · 4.69 KB
/
runtime.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
package tango
import (
"fmt"
)
// ExecutionStrategy defines the interface for different execution strategies.
type ExecutionStrategy[Services, State any] interface {
Execute(m *Machine[Services, State]) (*Response[Services, State], error)
Compensate(m *Machine[Services, State]) (*Response[Services, State], error)
}
// SequentialStrategy is a default implementation of ExecutionStrategy that runs steps sequentially.
type SequentialStrategy[Services, State any] struct{}
func (s *SequentialStrategy[Services, State]) Execute(m *Machine[Services, State]) (*Response[Services, State], error) {
for i := 0; i < len(m.Steps); i++ {
step := m.Steps[i]
response, err := m.executeStep(step)
if err != nil {
return nil, err
}
m.mu.Lock()
m.ExecutedSteps = append(m.ExecutedSteps, step)
m.Context.PreviousResult = response
m.mu.Unlock()
switch response.Status {
case NEXT:
continue
case DONE:
return response, nil
case ERROR:
cResponse, err := m.Compensate()
if err != nil {
return nil, fmt.Errorf("compensate error: %v", err)
}
return cResponse, fmt.Errorf("step %s failed: %v", step.Name, response.Result)
case SKIP:
i += response.SkipCount
case JUMP:
targetIndex := -1
for index, s := range m.Steps {
if s.Name == response.JumpTarget {
targetIndex = index
break
}
}
if targetIndex >= 0 {
i = targetIndex - 1
} else {
return nil, fmt.Errorf("jump target '%s' not found at %s", response.JumpTarget, step.Name)
}
}
}
return nil, nil
}
// Compensate runs the compensate functions of the executed steps.
func (s *SequentialStrategy[Services, State]) Compensate(m *Machine[Services, State]) (*Response[Services, State], error) {
m.Context = m.InitialContext
for i := len(m.ExecutedSteps) - 1; i >= 0; i-- {
step := m.ExecutedSteps[i]
if step.BeforeCompensate != nil {
if err := step.BeforeCompensate(m.Context); err != nil {
return nil, err
}
}
if step.Compensate == nil {
return nil, fmt.Errorf("step %s has no compensate function", step.Name)
}
if _, err := step.Compensate(m.Context); err != nil {
return nil, err
}
if step.AfterCompensate != nil {
if err := step.AfterCompensate(m.Context); err != nil {
return nil, err
}
}
}
return nil, nil
}
// ConcurrentStrategy runs steps concurrently.
type ConcurrentStrategy[Services, State any] struct {
Concurrency int
}
func (c *ConcurrentStrategy[Services, State]) Execute(m *Machine[Services, State]) (*Response[Services, State], error) {
if c.Concurrency <= 1 {
return (&SequentialStrategy[Services, State]{}).Execute(m)
}
sem := make(chan struct{}, c.Concurrency)
responseChan := make(chan *Response[Services, State], len(m.Steps))
errorChan := make(chan error, len(m.Steps))
for i := 0; i < len(m.Steps); i++ {
sem <- struct{}{}
go func(step Step[Services, State]) {
defer func() { <-sem }()
response, err := m.executeStep(step)
if err != nil {
errorChan <- err
return
}
responseChan <- response
m.mu.Lock()
m.ExecutedSteps = append(m.ExecutedSteps, step)
m.Context.PreviousResult = response
m.mu.Unlock()
}(m.Steps[i])
}
for i := 0; i < c.Concurrency; i++ {
sem <- struct{}{}
}
close(responseChan)
close(errorChan)
select {
case <-errorChan:
cResponse, err := m.Compensate()
if err != nil {
return nil, fmt.Errorf("compensate error: %v", err)
}
return cResponse, err
default:
}
for response := range responseChan {
if response.Status == DONE {
return response, nil
}
}
return nil, nil
}
// Compensate runs the compensate functions of the executed steps.
func (c *ConcurrentStrategy[Services, State]) Compensate(m *Machine[Services, State]) (*Response[Services, State], error) {
if c.Concurrency <= 1 {
return (&SequentialStrategy[Services, State]{}).Compensate(m)
}
sem := make(chan struct{}, c.Concurrency)
errorChan := make(chan error, len(m.ExecutedSteps))
for i := len(m.ExecutedSteps) - 1; i >= 0; i-- {
sem <- struct{}{}
go func(step Step[Services, State]) {
defer func() { <-sem }()
if step.BeforeCompensate != nil {
if err := step.BeforeCompensate(m.Context); err != nil {
errorChan <- err
return
}
}
if step.Compensate == nil {
errorChan <- fmt.Errorf("step %s has no compensate function", step.Name)
return
}
if _, err := step.Compensate(m.Context); err != nil {
errorChan <- err
return
}
if step.AfterCompensate != nil {
if err := step.AfterCompensate(m.Context); err != nil {
errorChan <- err
return
}
}
}(m.ExecutedSteps[i])
}
for i := 0; i < c.Concurrency; i++ {
sem <- struct{}{}
}
close(errorChan)
select {
case err := <-errorChan:
return nil, err
default:
return nil, nil
}
}