Documentation
ΒΆ
Index ΒΆ
- func All[T any](jobs []Job[T], opts ...BlockOption) ([]T, error)
- func Block(block func(n Nursery) error, opts ...BlockOption) (err error)
- func IsDone(ctx context.Context) bool
- func Map[T any, V any](input []T, f func(context.Context, T) (V, error), opts ...BlockOption) ([]V, error)
- func Map2[K comparable, V any](input map[K]V, f func(context.Context, K, V) (K, V, error), ...) (map[K]V, error)
- func MapInPlace[T any](input []T, f func(context.Context, T) (T, error), opts ...BlockOption) ([]T, error)
- func Race[T any](jobs []Job[T], opts ...BlockOption) (T, error)
- func Range[T any](seq iter.Seq[T], block func(context.Context, T) error, opts ...BlockOption) error
- func Range2[K, V any](seq iter.Seq2[K, V], block func(context.Context, K, V) error, ...) error
- func Sleep(ctx context.Context, d time.Duration)
- type BlockOption
- func WithCollectErrors(errors *[]error) BlockOption
- func WithContext(ctx context.Context) BlockOption
- func WithDeadline(d time.Time) BlockOption
- func WithErrorHandler(handler func(error)) BlockOption
- func WithIgnoreErrors() BlockOption
- func WithMaxGoroutines(max int) BlockOption
- func WithTimeout(timeout time.Duration) BlockOption
- type GoroutinePanic
- type Job
- type Nursery
- type Routine
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func All ΒΆ
func All[T any](jobs []Job[T], opts ...BlockOption) ([]T, error)
All executes all jobs in separate goroutines and stores each result in the returned slice.
func Block ΒΆ
func Block(block func(n Nursery) error, opts ...BlockOption) (err error)
Block starts a nursery block that returns when all goroutines have returned. If a goroutine returns an error, it is returned after context is canceled unless a custom error handler is provided. In case of a panic context is canceled and panic is immediately forwarded without waiting for other goroutines to handle context cancellation. Error returned by block closure always trigger a context cancellation and is returned if it occurs before a default goroutine error handler is called. Calling Nursery.Go() after end of block always panic. Calling Nursery.Go after context is canceled still runs the provided function, you're responsible for handling cancellation.
func Map ΒΆ
func Map[T any, V any](input []T, f func(context.Context, T) (V, error), opts ...BlockOption) ([]V, error)
Map applies f to each element of input and returns a new slice containing mapped results.
func Map2 ΒΆ
func Map2[K comparable, V any](input map[K]V, f func(context.Context, K, V) (K, V, error), opts ...BlockOption) (map[K]V, error)
Map2 applies f to each key, value pair of input and returns a new slice containing mapped results.
func MapInPlace ΒΆ
func MapInPlace[T any](input []T, f func(context.Context, T) (T, error), opts ...BlockOption) ([]T, error)
MapInPlace applies f to each element of input and returns modified input slice.
func Race ΒΆ added in v0.4.0
func Race[T any](jobs []Job[T], opts ...BlockOption) (T, error)
Race executes all jobs in separate goroutines and returns first result. Remaining goroutines are canceled.
Types ΒΆ
type BlockOption ΒΆ
type BlockOption func(cfg *nursery)
BlockOption define an option function applied to a nursery block.
func WithCollectErrors ΒΆ added in v0.4.0
func WithCollectErrors(errors *[]error) BlockOption
WithCollectErrors returns a nursery block option that sets error handler to collect goroutine errors into provided error slice. Provided error slice must not be read and write until end of block.
func WithContext ΒΆ
func WithContext(ctx context.Context) BlockOption
WithContext returns a nursery block option that replaces nursery context with the given one.
func WithDeadline ΒΆ
func WithDeadline(d time.Time) BlockOption
WithDeadline returns a nursery block option that wraps nursery context with a new one that will be canceled at `d`.
func WithErrorHandler ΒΆ
func WithErrorHandler(handler func(error)) BlockOption
WithErrorHandler returns a nursery block option that adds an error handler to the block. Provided error handler is executed in the goroutine that returned the error.
func WithIgnoreErrors ΒΆ added in v0.4.0
func WithIgnoreErrors() BlockOption
WithIgnoreErrors returns a nursery block option that sets error handler to a noop function.
func WithMaxGoroutines ΒΆ
func WithMaxGoroutines(max int) BlockOption
WithMaxGoroutines returns a nursery block option that limits the maximum number of goroutine running concurrently. If max is zero, number of goroutine is unlimited. This function panics if max is negative.
func WithTimeout ΒΆ
func WithTimeout(timeout time.Duration) BlockOption
WithTimeout returns a nursery block option that wraps nursery context with a new one that timeout after the given duration.
type GoroutinePanic ΒΆ
GoroutinePanic holds value from a recovered panic along a stacktrace.
func (GoroutinePanic) String ΒΆ
func (gp GoroutinePanic) String() string
String implements fmt.Stringer.
func (GoroutinePanic) Unwrap ΒΆ
func (gp GoroutinePanic) Unwrap() error
Unwrap unwrap underlying error.
type Nursery ΒΆ
type Nursery interface {
context.Context
// Executes provided [Routine] as soon as possible in a separate goroutine.
Go(Routine)
}
Nursery is a supervisor that executes goroutines and manages their lifecycle. It embeds a context.Context to provide cancellation and deadlines to all spawned goroutines. When the nursery's context is canceled, all goroutines are signaled to stop via the context cancellation.

