work

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package work provides structured concurrency primitives with bounded parallelism and OpenTelemetry tracing. It offers Map, All, Race, and Stream patterns for fan-out/fan-in workloads.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(ctx context.Context, tasks []func(context.Context) error, opts ...Option) error

All runs all tasks with bounded concurrency. Returns *Errors if any fail.

func Map

func Map[T, R any](ctx context.Context, items []T, fn func(context.Context, T) (R, error), opts ...Option) ([]R, error)

Map applies fn to each item with bounded concurrency. Results are returned in input order. If any items fail, returns *Errors with all failures.

func Race

func Race[R any](ctx context.Context, tasks ...func(context.Context) (R, error)) (R, error)

Race launches all tasks concurrently and returns the result of the first one to succeed. If all tasks fail, returns *Errors. The context passed to tasks is cancelled once a winner is found.

func Stream

func Stream[T, R any](ctx context.Context, in <-chan T, fn func(context.Context, T) (R, error), opts ...Option) <-chan Result[R]

Stream applies fn to values received from in with bounded concurrency, sending results to the returned channel. The output channel is closed when the input channel is closed and all in-flight work completes.

Types

type Errors

type Errors struct {
	Failures []Failure
}

Errors collects per-item failures from Map or All.

func (*Errors) Error

func (e *Errors) Error() string

func (*Errors) Unwrap

func (e *Errors) Unwrap() []error

Unwrap returns the underlying errors for use with errors.Is / errors.As.

type Failure

type Failure struct {
	Index int
	Err   error
}

Failure records the index and error of a single failed task.

type Option

type Option func(*config)

Option configures a work function.

func Workers

func Workers(n int) Option

Workers sets the maximum concurrency level. Values less than 1 are clamped to 1.

type Result

type Result[T any] struct {
	Value T
	Err   error
	Index int
}

Result holds the outcome of processing a single item.

Jump to

Keyboard shortcuts

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