concurrency

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 6 Imported by: 1

README

A collection of types and functions for structured concurrency

PkgGoDev GHA Build Go Report Card Slack chat

This package centres around a managed tree of goroutines. An error in any goroutine or sub-tree will cancel the entire tree. Each node in the tree is a group of goroutines that can be waited on.

There are also useful concurrent functions that utilise the tree.

Documentation

Overview

Package concurrency provides types and functions for managing concurrency in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call added in v0.0.2

func Call(ctx context.Context, fn func() error) context.Context

Call runs fn in a separate goroutine and returns a context that will cancel when the function completes.

func Map

func Map[U, T any](tree *Tree, values []U, fn func(context.Context, U) (T, error)) ([]T, error)

Map runs fn in tree for each value in values, and returns the results.

Order is preserved. Each call will run in a separate Tree.Go() so use WithConcurrencyLimit() if necessary

func NoJitter

func NoJitter() time.Duration

func Schedule

func Schedule(tree *Tree, fn func(context.Context) (time.Duration, error)) error

Schedule calls fn every time interval until it returns an error or the context is cancelled.

Types

type Channel

type Channel[T any] struct {
	// contains filtered or unexported fields
}

Channel utilises a tree to produce values and send them to a channel.

func ToChannel

func ToChannel[T any](ctx context.Context, dest chan<- T, options ...Option) (*Channel[T], context.Context)

ToChannel creates a new Channel instance.

func (*Channel[T]) Go

func (v *Channel[T]) Go(fn func(context.Context) (T, error))

func (*Channel[T]) Sub

func (v *Channel[T]) Sub(fn func(context.Context, *Channel[T]) error)

func (*Channel[T]) Wait

func (v *Channel[T]) Wait() error

type Option

type Option func(*Tree)

func WithConcurrencyLimit

func WithConcurrencyLimit(n int) Option

WithConcurrencyLimit sets the maximum number of goroutines that will be executed concurrently by the tree before blocking.

A value of 0 disables the limit.

func WithJitter

func WithJitter(fn func() time.Duration) Option

WithJitter sets the jitter function used to delay the start of each goroutine.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

A Tree manages calling a set of functions returning errors, with optional concurrency limits. trees can be arranged in a tree.

Panics in functions are recovered and cause the tree to be cancelled.

func New

func New(ctx context.Context, options ...Option) (*Tree, context.Context)

New creates a new Tree.

func (*Tree) Go

func (g *Tree) Go(fn func(context.Context) error)

Go runs fn in a goroutine, and cancels the tree if any function returns an error.

The context passed to fn is a child of the context passed to New. A new sub-tree can be created from this context by calling treeFromContext.

func (g *Tree) Link(waiter Waiter)

Link an existing Waiter to the tree.

Useful for eg. syncing on an errgroup, or a separate Tree.

func (*Tree) Sub

func (g *Tree) Sub(fn func(context.Context, *Tree) error, options ...Option)

Sub calls fn in a new goroutine with a new sub-tree.

The sub-tree will inherit the options of the parent tree, but can override them.

Wait() is automatically called on the sub-tree when fn returns.

func (*Tree) Wait

func (g *Tree) Wait() error

Wait for the tree to finish, and return the results of all successful calls.

Results will be returned in the order in which Go() was called. Failing taks will leave zero values in the result slice.

Unlike errtree this will return the first error returned by a user function, not context.Canceled.

type Waiter

type Waiter interface {
	Wait() error
}

A Waiter is a type that can wait for completion.

Jump to

Keyboard shortcuts

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