Documentation ¶
Index ¶
- func Consumers[T any](ctx context.Context, n int, f func(context.Context, int, T) error) (func(T) error, func() error)
- func Pool[T, U any](n int, f func(T) (U, error)) func(T) (U, error)
- func Producers[T any](ctx context.Context, n int, f func(context.Context, int, func(T) error) error) func() (T, bool, error)
- func Values[T any](ctx context.Context, n int, f func(context.Context, int) (T, error)) ([]T, error)
- type Error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Consumers ¶
func Consumers[T any](ctx context.Context, n int, f func(context.Context, int, T) error) (func(T) error, func() error)
Consumers launches n parallel workers each consuming values supplied by the caller.
When a value is available, an available worker calls the function f to consume it. This callback receives the worker's number (in the range 0 through n-1) and the value.
An error from any worker cancels them all.
The caller receives two callbacks: one for sending a value to the workers, and one for closing that channel (signaling the end of input and causing the workers to exit normally).
func Pool ¶
Pool permits up to n concurrent calls to a function f. The caller receives a callback for requesting a worker from this pool. When no worker is available, the callback blocks until one becomes available. Then it invokes f and returns the result.
Each call of the callback is synchronous. Any desired concurrency is the responsibility of the caller.
func Producers ¶
func Producers[T any](ctx context.Context, n int, f func(context.Context, int, func(T) error) error) func() (T, bool, error)
Producers launches n parallel workers each running the function f.
Each worker receives its worker number (in the range 0 through n-1) and a callback to use for producing a value. If the callback returns an error, the worker should exit with that error.
An error from any worker cancels them all.
The caller gets a callback for consuming the values produced. Each call of the callback yields a value, a boolean, and an error. If the error is non-nil, it has propagated out from one of the workers and no more data may be consumed. Otherwise, if the boolean is false, the workers have completed and there is no more output to consume. Otherwise, the value is a valid next value.
Example:
consume := Producers(ctx, 10, produceVals) for { val, ok, err := consume() if err != nil { panic(err) } if !ok { break } ...handle val... }
func Values ¶
func Values[T any](ctx context.Context, n int, f func(context.Context, int) (T, error)) ([]T, error)
Values produces a slice of n values using n parallel workers each running the function f.
Each worker receives its worker number (in the range 0 through n-1).
An error from any worker cancels them all. The first error is returned to the caller.
The resulting slice has length n. The value at position i comes from worker i.