-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.go
243 lines (211 loc) · 4.24 KB
/
do.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
package transformer
import (
"cloud.google.com/go/datastore"
"context"
"errors"
"google.golang.org/api/iterator"
"sync"
"time"
)
// New creates a new transformer Doer
func New(cli *datastore.Client, query *datastore.Query, opts ...Option) (*Doer, error) {
d := &Doer{
cli: cli,
query: query,
workers: 1,
tasks: make(chan *Entity),
failOnErr: true,
}
for _, opt := range opts {
if err := opt(d); err != nil {
return nil, err
}
}
return d, nil
}
// Doer runs the query and sends to the workers the entities
// to transform and save.
type Doer struct {
cli *datastore.Client
query *datastore.Query
transformers []Transformer
workers int
wg sync.WaitGroup
tasks chan *Entity
// failOnErr tells if return when a worker error occurs.
// Default is true
failOnErr bool
notifyErrCh []chan error
}
// Apply adds Transformer to be applied to an Entity
func (d *Doer) Apply(t ...Transformer) {
d.transformers = append(d.transformers, t...)
}
func (d *Doer) NotifyErr() <-chan error {
ch := make(chan error)
d.notifyErrCh = append(d.notifyErrCh, ch)
return ch
}
// Do queries the entities and sends to the workers
func (d *Doer) Do(ctx context.Context) error {
errNotifier := newErrorNotifier()
defer func() {
close(d.tasks)
d.wg.Wait()
ctxT, _ := context.WithTimeout(ctx, time.Second*10)
errNotifier.close(ctxT)
}()
// Init workers
d.wg.Add(d.workers)
var workersErrCh []<-chan error
for i := 0; i < d.workers; i++ {
w := &worker{
wg: &d.wg,
cli: d.cli,
tasks: d.tasks,
transformers: d.transformers,
}
workersErrCh = append(workersErrCh, w.run(ctx))
}
// Workers error handling
var failCh <-chan error
if d.failOnErr {
failCh = d.NotifyErr()
}
errNotifier.notify(workersErrCh, d.notifyErrCh)
// Iterate entities
it := d.cli.Run(ctx, d.query)
for {
var entity = newEntity()
_, err := it.Next(entity)
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return err
}
if d.failOnErr {
select {
case err := <-failCh:
return err
default:
}
}
// Send to workers
select {
case <-ctx.Done():
return ctx.Err()
case d.tasks <- entity:
}
}
return nil
}
// worker applies transformers to the entities and save it
type worker struct {
wg *sync.WaitGroup
cli *datastore.Client
tasks <-chan *Entity
transformers []Transformer
}
func (w *worker) run(ctx context.Context) chan error {
errCh := make(chan error)
go func() {
defer close(errCh)
defer w.wg.Done()
for {
select {
case <-ctx.Done():
return
case task, ok := <-w.tasks:
if !ok {
return
}
if err := w.process(ctx, task); err != nil {
select {
case <-ctx.Done():
return
case errCh <- err:
}
}
}
}
}()
return errCh
}
func (w *worker) process(ctx context.Context, task *Entity) error {
for _, t := range w.transformers {
if err := t.Transform(task); err != nil {
return err
}
}
if _, err := w.cli.Put(ctx, task.key, task); err != nil {
return err
}
return nil
}
type errorNotifier struct {
stop chan struct{}
done chan struct{}
once sync.Once
}
func newErrorNotifier() *errorNotifier {
return &errorNotifier{
stop: make(chan struct{}),
done: make(chan struct{}),
}
}
func (n *errorNotifier) notify(in []<-chan error, out []chan error) {
n.once.Do(func() {
go func() {
errCh := make(chan error)
var wg sync.WaitGroup
wg.Add(len(in))
for i := 0; i < len(in); i++ {
go func(inCh <-chan error) {
defer wg.Done()
for {
select {
case <-n.stop:
return
case err, ok := <-inCh:
if !ok {
return
}
select {
case <-n.stop:
return
case errCh <- err:
}
}
}
}(in[i])
}
go func() {
defer func() {
for _, o := range out {
close(o)
}
close(n.done)
}()
for err := range errCh {
for i := 0; i < len(out); i++ {
select {
case <-n.stop:
return
case out[i] <- err:
}
}
}
}()
wg.Wait()
close(errCh)
}()
})
}
func (n *errorNotifier) close(ctx context.Context) {
select {
case <-ctx.Done():
close(n.stop)
case <-n.done:
}
}