Last active
August 17, 2020 02:58
-
-
Save matryer/20e2ea821f9df470377084eda6ef0dcb to your computer and use it in GitHub Desktop.
Async helper function for Go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Released under MIT License | |
package main | |
import "sync" | |
// asyncFn is a function that can be run asychronously by | |
// async. | |
type asyncFn func() error | |
// async runs many functions at the same time. | |
// Returns the last not-nil error. | |
func async(fns ...asyncFn) error { | |
var errLock sync.Mutex // protects errResult | |
var errResult error | |
var wg sync.WaitGroup | |
wg.Add(len(fns)) | |
for i := range fns { | |
go func(i int) { | |
defer wg.Done() | |
if err := fns[i](); err != nil { | |
errLock.Lock() | |
errResult = err | |
errLock.Unlock() | |
} | |
}(i) | |
} | |
wg.Wait() | |
if errResult != nil { | |
return errResult | |
} | |
return nil | |
} |
👍
Here is my approach to the same need, but inspired from Java executors somehow. In some scenarios it becomes useful, Specially when you are going to handle a lot of concurrent functions that need to work with same data (High contention rate) or when you are doing batch jobs and don’t want to bother the go runtime.
I have an errutil.ExecParallel function that is similar, but it relies on errutil.Slice, which can turn a []error
into a Multierror. This way you capture all the errors, not just the first.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This
async
function is a helper that lets you provide manyfunc() error
functions, and it will run them all at the same time (each in their own goroutine).It saves you the effort of using the
sync
package to handleMutex
andWaitGroup
types. Instead, just create little local functions, and run them at the same time.Be careful not to trample any state. Each function should only work with its own dedicated variables, otherwise you'll get data races.
In this design, the functions return an
error
, the last of which is returned fromasync
itself. If you care about each error, handle it explicitly inside your anonymous function (like we do withbigFile
andmapData
in the example below).Example using
async
(Released under MIT License.)