forked from sajari/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalk.go
65 lines (55 loc) · 1.36 KB
/
walk.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
package storage
import (
"sync"
"golang.org/x/net/context"
)
// WalkFn is a function type which is passed to Walk.
type WalkFn func(path string) error
// Walker is an interface which defines the Walk method.
type Walker interface {
// Walk traverses a path listing by prefix, calling fn with each path.
Walk(ctx context.Context, path string, fn WalkFn) error
}
// List runs the Walker on the given path and returns the list of visited paths.
func List(ctx context.Context, w Walker, path string) ([]string, error) {
var out []string
if err := w.Walk(ctx, path, func(path string) error {
out = append(out, path)
return nil
}); err != nil {
return nil, err
}
return out, nil
}
// WalkN creates n workers which accept paths from the Walker. If a WalkFn
// returns non-nil error we wait for other running WalkFns to finish before
// returning.
func WalkN(ctx context.Context, w Walker, path string, n int, fn WalkFn) error {
errCh := make(chan error, n)
ch := make(chan string)
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
for f := range ch {
if err := fn(f); err != nil {
errCh <- err
break
}
}
wg.Done()
}()
}
err := w.Walk(ctx, path, func(path string) error {
select {
case err := <-errCh:
return err
case ch <- path:
return nil
}
})
close(ch)
wg.Wait()
close(errCh)
return err
}