| OLD | NEW |
| (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 } | |
| OLD | NEW |