go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/sync/parallel/run.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package parallel 16 17 // Run executes a generator function, dispatching each generated task to the 18 // Runner. Run returns immediately with an error channel that can be used to 19 // reap the results of those tasks. 20 // 21 // The returned error channel must be consumed, or it can block additional 22 // functions from being run from gen. A common consumption function is 23 // errors.MultiErrorFromErrors, which will buffer all non-nil errors into an 24 // errors.MultiError. Other functions to consider are Must and Ignore (in this 25 // package). 26 // 27 // Note that there is no association between error channel's error order and 28 // the generated task order. However, the channel will return exactly one error 29 // result for each generated task. 30 // 31 // If workers is <= 0, it will be unbounded; otherwise, a pool of at most 32 // workers sustained goroutines will be used to execute the task. 33 func Run(workers int, gen func(chan<- func() error)) <-chan error { 34 r := Runner{ 35 Maximum: workers, 36 Sustained: workers, 37 } 38 return r.runThen(gen, r.Close) 39 }