| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package errgroup_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"crypto/md5" |
| 10 |
"fmt" |
| 11 |
"log" |
| 12 |
"os" |
| 13 |
"path/filepath" |
| 14 |
|
| 15 |
"golang.org/x/sync/errgroup" |
| 16 |
) |
| 17 |
|
| 18 |
// Pipeline demonstrates the use of a Group to implement a multi-stage |
| 19 |
// pipeline: a version of the MD5All function with bounded parallelism from |
| 20 |
// https://blog.golang.org/pipelines. |
| 21 |
func ExampleGroup_pipeline() { |
| 22 |
m, err := MD5All(context.Background(), ".") |
| 23 |
if err != nil { |
| 24 |
log.Fatal(err) |
| 25 |
} |
| 26 |
|
| 27 |
for k, sum := range m { |
| 28 |
fmt.Printf("%s:\t%x\n", k, sum) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
type result struct { |
| 33 |
path string |
| 34 |
sum [md5.Size]byte |
| 35 |
} |
| 36 |
|
| 37 |
// MD5All reads all the files in the file tree rooted at root and returns a map |
| 38 |
// from file path to the MD5 sum of the file's contents. If the directory walk |
| 39 |
// fails or any read operation fails, MD5All returns an error. |
| 40 |
func MD5All(ctx context.Context, root string) (map[string][md5.Size]byte, error) { |
| 41 |
// ctx is canceled when g.Wait() returns. When this version of MD5All returns |
| 42 |
// - even in case of error! - we know that all of the goroutines have finished |
| 43 |
// and the memory they were using can be garbage-collected. |
| 44 |
g, ctx := errgroup.WithContext(ctx) |
| 45 |
paths := make(chan string) |
| 46 |
|
| 47 |
g.Go(func() error { |
| 48 |
defer close(paths) |
| 49 |
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
| 50 |
if err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
if !info.Mode().IsRegular() { |
| 54 |
return nil |
| 55 |
} |
| 56 |
select { |
| 57 |
case paths <- path: |
| 58 |
case <-ctx.Done(): |
| 59 |
return ctx.Err() |
| 60 |
} |
| 61 |
return nil |
| 62 |
}) |
| 63 |
}) |
| 64 |
|
| 65 |
// Start a fixed number of goroutines to read and digest files. |
| 66 |
c := make(chan result) |
| 67 |
const numDigesters = 20 |
| 68 |
for i := 0; i < numDigesters; i++ { |
| 69 |
g.Go(func() error { |
| 70 |
for path := range paths { |
| 71 |
data, err := os.ReadFile(path) |
| 72 |
if err != nil { |
| 73 |
return err |
| 74 |
} |
| 75 |
select { |
| 76 |
case c <- result{path, md5.Sum(data)}: |
| 77 |
case <-ctx.Done(): |
| 78 |
return ctx.Err() |
| 79 |
} |
| 80 |
} |
| 81 |
return nil |
| 82 |
}) |
| 83 |
} |
| 84 |
go func() { |
| 85 |
g.Wait() |
| 86 |
close(c) |
| 87 |
}() |
| 88 |
|
| 89 |
m := make(map[string][md5.Size]byte) |
| 90 |
for r := range c { |
| 91 |
m[r.path] = r.sum |
| 92 |
} |
| 93 |
// Check whether any of the goroutines failed. Since g is accumulating the |
| 94 |
// errors, we don't need to send them (or check for them) in the individual |
| 95 |
// results sent on the channel. |
| 96 |
if err := g.Wait(); err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
return m, nil |
| 100 |
} |