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

Side by Side Diff: common/sync/parallel/workPool.go

Issue 2958433002: [errors] Remove Fix(), move MultiErrorFromErrors(). (Closed)
Patch Set: Created 3 years, 6 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
« no previous file with comments | « common/sync/parallel/multierror_test.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package parallel 5 package parallel
6 6
7 import ( 7 import (
8 "github.com/luci/luci-go/common/errors" 8 "github.com/luci/luci-go/common/errors"
9 ) 9 )
10 10
11 // WorkPool creates a fixed-size pool of worker goroutines. A supplied generator 11 // WorkPool creates a fixed-size pool of worker goroutines. A supplied generator
12 // method creates task functions and passes them through to the work pool. 12 // method creates task functions and passes them through to the work pool.
13 // 13 //
14 // WorkPool will use at most workers goroutines to execute the supplied tasks. 14 // WorkPool will use at most workers goroutines to execute the supplied tasks.
15 // If workers is <= 0, WorkPool will be unbounded and behave like FanOutIn. 15 // If workers is <= 0, WorkPool will be unbounded and behave like FanOutIn.
16 // 16 //
17 // WorkPool blocks until all the generator completes and all workers have 17 // WorkPool blocks until all the generator completes and all workers have
18 // finished their tasks. 18 // finished their tasks.
19 func WorkPool(workers int, gen func(chan<- func() error)) error { 19 func WorkPool(workers int, gen func(chan<- func() error)) error {
20 » return errors.MultiErrorFromErrors(Run(workers, gen)) 20 » return MultiErrorFromErrors(Run(workers, gen))
21 } 21 }
22 22
23 // FanOutIn is useful to quickly parallelize a group of tasks. 23 // FanOutIn is useful to quickly parallelize a group of tasks.
24 // 24 //
25 // You pass it a function which is expected to push simple `func() error` 25 // You pass it a function which is expected to push simple `func() error`
26 // closures into the provided chan. Each function will be executed in parallel 26 // closures into the provided chan. Each function will be executed in parallel
27 // and their error results will be collated. 27 // and their error results will be collated.
28 // 28 //
29 // The function blocks until all functions are executed, and an 29 // The function blocks until all functions are executed, and an
30 // errors.MultiError is returned if one or more of your fan-out tasks failed, 30 // errors.MultiError is returned if one or more of your fan-out tasks failed,
31 // otherwise this function returns nil. 31 // otherwise this function returns nil.
32 // 32 //
33 // This function is equivalent to WorkPool(0, gen). 33 // This function is equivalent to WorkPool(0, gen).
34 func FanOutIn(gen func(chan<- func() error)) error { 34 func FanOutIn(gen func(chan<- func() error)) error {
35 return WorkPool(0, gen) 35 return WorkPool(0, gen)
36 } 36 }
37
38 // MultiErrorFromErrors takes an error-channel, blocks on it, and returns
39 // a MultiError for any errors pushed to it over the channel, or nil if
40 // all the errors were nil.
41 func MultiErrorFromErrors(ch <-chan error) error {
Vadim Sh. 2017/06/23 00:32:16 does it have to be public? Anyone uses it besides
42 if ch == nil {
43 return nil
44 }
45 ret := errors.MultiError(nil)
46 for e := range ch {
47 if e == nil {
48 continue
49 }
50 ret = append(ret, e)
51 }
52 if len(ret) == 0 {
53 return nil
54 }
55 return ret
56 }
OLDNEW
« no previous file with comments | « common/sync/parallel/multierror_test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698