Skip to content

(Go) ⏩ Run many functions in parallel, but fast-bail on errors.

Notifications You must be signed in to change notification settings

hackergrrl/parallel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

parallel

Execute many functions in parallel. Immediately return if one returns an error.

Background

A common Go pattern is to execute several independent tasks in parallel and wait for them to all complete before proceeding. However, the tasks tend to be all-or-nothing, and you'll want the whole thing to bail if any of them return an error.

Examples

Say you have a few long-ish running functions that return errors:

import (
  "errors"
  "fmt"
  parallel "github.com/noffle/parallel"
)

func main() {
  err := parallel.Parallel(nil, sleep, sleep)
  fmt.Println(err)
}

func sleep() error {
  fmt.Printf("sleeping..\n")
  time.Sleep(100 * time.Millisecond)
  fmt.Printf("..slept!\n")
  return errors.New("awoke")
}
sleeping..
sleeping..
..slept!
..slept!
awoke

You can also provide a chan struct{} as the first parameter to cancel the parallel operation early:

func main() {
  var bail = make(chan struct{})

  parallel.Parallel(bail,
    sleep,
    func() error {
      fmt.Printf("going to bail..\n")
      time.Sleep(50 * time.Millisecond)
      close(bail)
      fmt.Printf("..bailed!\n")
    },
  )
}

outputs

sleeping..
going to bail..
..bailed!

parallel is also compatible with golang.org/x/net/context!

func main() {
  ctx, bail := context.WithCancel(context.Background())

  parallel.Parallel(ctx.Done(),
    sleep,
    func() error {
      time.Sleep(50 * time.Millisecond)
      bail()
    },
  )
}

API

import "github.com/noffle/parallel"

func Parallel(chan struct{}, ...func() error) error

Accepts an optional channel to signal cancellation of the parallel tasks, and a variable number of functions matching the signature func() error.

The first function to return a non-nil error closes the cancel channel (if non-nil) and propogates its error as the return value.

In the case that the cancel channel is closed, Parallel will return parallel.Canceled.

Install

go get github.com/noffle/parallel

License

MIT

About

(Go) ⏩ Run many functions in parallel, but fast-bail on errors.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages