# README

Go library for structured concurrency

go.dev reference

Structured concurrency helps reasoning about the behaviour of parallel programs. parallel implements structured concurrency for Go.

func subtask(ctx context.Context) error {
    // to be run in parallel
}

type subtaskWithData struct { /* ... * / }

func (swd *subtaskWithData) Run(ctx context.Context) error {
    // to be run in parallel
}

err := parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
    swd := &subtaskWithData{}

    // do some synchronous initialization here

    spawn("subtask", parallel.Fail, subtask)
    spawn("subtaskWithData", parallel.Fail, swd.Run)
    return nil
})

Runs initializaiton within parallel.Run(), and then waits until context is canceled, or one of spawned tasks finishes. Panics in goroutines are captured.

See the documentation for additional features:

  • subprocess groups without inversion of control
  • tasks that may exit and keep the group running
  • tasks that may exit and cause the group to stop gracefully

Legal

Copyright Tectonic Networks, Inc.

Authors:

# Functions

NewGroup creates a new Group controlled by the given context.
NewSubgroup creates a new Group nested within another.
Run runs a task with several subtasks.

# Constants

Continue means other subtasks of the parent task should continue to run.
Exit means shut down the parent task gracefully.
Fail means shut down the parent task gracefully and return an error.

# Structs

ErrPanic is the error type that occurs when a subtask panics.
Group is a facility for running a task with several subtasks without inversion of control.

# Type aliases

OnExit is an enumeration of exit handling modes.
SpawnFn is a function that starts a subtask in a goroutine.
A Task is the main function of a service or component, or any other task.