Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: go/src/infra/libs/parallel/parallel.go

Issue 1153883002: go: infra/libs/* now live in luci-go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: move the rest too Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package parallel
6
7 import (
8 "sync"
9
10 "infra/libs/errors"
11 )
12
13 // FanOutIn is useful to quickly parallelize a group of tasks.
14 //
15 // You pass it a function which is expected to push simple `func() error`
16 // closures into the provided chan. Each function will be executed in parallel
17 // and their error results will be collated.
18 //
19 // The function blocks until all functions are executed, and an
20 // errors.MultiError is returned if one or more of your fan-out tasks failed,
21 // otherwise this function returns nil.
22 func FanOutIn(gen func(chan<- func() error)) error {
23 funchan := make(chan func() error)
24 go func() {
25 defer close(funchan)
26 gen(funchan)
27 }()
28
29 errchan := make(chan error)
30 grp := sync.WaitGroup{}
31 for fn := range funchan {
32 grp.Add(1)
33 fn := fn
34 go func() {
35 defer grp.Done()
36 if err := fn(); err != nil {
37 errchan <- err
38 }
39 }()
40 }
41 go func() {
42 grp.Wait()
43 close(errchan)
44 }()
45 return errors.MultiErrorFromErrors(errchan)
46 }
OLDNEW
« no previous file with comments | « go/src/infra/libs/logging/memlogger/memory_test.go ('k') | go/src/infra/libs/parallel/parallel_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698