-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpipeline_tree_mkdirer.go
61 lines (50 loc) · 1.13 KB
/
pipeline_tree_mkdirer.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
//go:build !tinywasm
package gtree
import (
"context"
"sync"
)
func newMkdirerPipeline(dir string, fileExtensions []string) mkdirerPipeline {
return &defaultMkdirerPipeline{
defaultMkdirerSimple: newMkdirerSimple(dir, fileExtensions).(*defaultMkdirerSimple),
}
}
type defaultMkdirerPipeline struct {
*defaultMkdirerSimple
}
const workerMkdirNum = 10
func (dm *defaultMkdirerPipeline) mkdir(ctx context.Context, roots <-chan *Node) <-chan error {
errc := make(chan error, 1)
go func() {
defer close(errc)
wg := &sync.WaitGroup{}
for i := 0; i < workerMkdirNum; i++ {
wg.Add(1)
go dm.worker(ctx, wg, roots, errc)
}
wg.Wait()
}()
return errc
}
func (dm *defaultMkdirerPipeline) worker(ctx context.Context, wg *sync.WaitGroup, roots <-chan *Node, errc chan<- error) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case root, ok := <-roots:
if !ok {
return
}
if dm.isExistRoot([]*Node{root}) {
errc <- ErrExistPath
return
}
if err := dm.makeDirectoriesAndFiles(root); err != nil {
errc <- err
return
}
}
}
}
var _ mkdirerPipeline = (*defaultMkdirerPipeline)(nil)