-
Notifications
You must be signed in to change notification settings - Fork 8
/
pipeline_tree_walker.go
56 lines (46 loc) · 1023 Bytes
/
pipeline_tree_walker.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
//go:build !tinywasm
package gtree
import (
"context"
"sync"
)
type defaultWalkerPipeline struct {
*defaultWalkerSimple
}
func newWalkerPipeline() walkerPipeline {
return &defaultWalkerPipeline{
defaultWalkerSimple: &defaultWalkerSimple{},
}
}
const workerWalkerNum = 10
func (dw *defaultWalkerPipeline) walk(ctx context.Context, roots <-chan *Node, callback func(*WalkerNode) error) <-chan error {
errc := make(chan error, 1)
go func() {
defer func() {
close(errc)
}()
wg := &sync.WaitGroup{}
for i := 0; i < workerWalkerNum; i++ {
wg.Add(1)
go dw.worker(ctx, wg, roots, callback, errc)
}
wg.Wait()
}()
return errc
}
func (dw *defaultWalkerPipeline) worker(ctx context.Context, wg *sync.WaitGroup, roots <-chan *Node, callback func(*WalkerNode) error, errc chan<- error) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case root, ok := <-roots:
if !ok {
return
}
if err := dw.walkNode(root, callback); err != nil {
errc <- err
}
}
}
}