Documentation
¶
Overview ¶
Package concurrency provides types and functions for managing concurrency in Go.
Index ¶
- func Call(ctx context.Context, fn func() error) context.Context
- func Map[U, T any](tree *Tree, values []U, fn func(context.Context, U) (T, error)) ([]T, error)
- func NoJitter() time.Duration
- func Schedule(tree *Tree, fn func(context.Context) (time.Duration, error)) error
- type Channel
- type Option
- type Tree
- type Waiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Call ¶ added in v0.0.2
Call runs fn in a separate goroutine and returns a context that will cancel when the function completes.
func Map ¶
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
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.
type Option ¶
type Option func(*Tree)
func WithConcurrencyLimit ¶
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 ¶
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 (*Tree) Go ¶
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 (*Tree) Link ¶
Link an existing Waiter to the tree.
Useful for eg. syncing on an errgroup, or a separate Tree.
func (*Tree) Sub ¶
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 ¶
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.