parallel

package module
v0.0.0-...-40f49bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2021 License: MIT Imports: 2 Imported by: 0

README

parallel

parallel is a tiny utility package for running functions in parallel with error handling. You can very easily orchestrate highly complex function executions that run in parallel or ordered fashion, combine both and get a single standard Go error back that supports functions from Go's errors package like errors.Is and errors.As.

Install

go get -u github.com/robinbraemer/parallel

Example

Imagine you need to run multiple functions in parallel but some of them in order. It would be quiet code heavy to do so in a clean manner.

Luckily you can use the parallel package to de-complicate this:

var fns [][]Fn
// ...
err := Ordered(
    Parallel( // A
        Parallel(fns[0]...), // 1
        Parallel(fns[1]...), // 2
        Parallel(fns[2]...), // 3
    ), 
    Parallel(fns[3]...),  // B
    Ordered(
        Parallel(fns[5]...), // 4
        Parallel(fns[6]...), // 5
    ),  // C
).Do()

This is what happens:

  • Ordered runs A, B, C in unparalleled order and returns the first error encountered
  • A runs 3 slices of functions in parallel and block until all are finished
  • after A is done B runs functions in parallel
  • after B is done C runs functions in parallel
  • Ordered returns

That means 1-3 must complete before A returns and B is run and so forth.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Do

type Do interface{ Do() error }

Do is the interface for an executable function/task.

func Ordered

func Ordered(do ...Do) Do

Ordered returns a Do that when run executes all passed Do's in order and blocks until all are done returning nil or the first error occurred.

func Parallel

func Parallel(do ...Do) Do

Parallel returns a Do that when run executes all passed Do's in parallel and blocks until all are done returning nil, one error or a multi-error.

type Fn

type Fn func() error

Fn implements Do interface.

func (Fn) Do

func (fn Fn) Do() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL