Bounded Concurrency¶
Automi executes concurrent transformations and side effects with bounded worker pools, child-context sibling cancellation, and recovered worker panics.

See API Enrichment & DLQ for ordered ParMap usage and Alerts, FanOut & ParEach for concurrent terminal dispatch with ParEach.
ParMap — Concurrent Per-Item Transform¶
(s Stream[T]) ParMap[U any](fn func(context.Context, T) (U, error), opts ...ParOption) Stream[U] fans items across a fixed pool of worker goroutines. Any Step[T, U] can be passed directly as fn. Passing nil yields ErrNilFunc.
Options and Defaults¶
| Option | Default | Effect |
|---|---|---|
Workers(n int) |
max(runtime.GOMAXPROCS(0), 1) |
Sets worker goroutine count (values <= 0 keep default). |
Ordered() |
unordered | Emits outputs in original input sequence using a bounded in-flight window. |
BufferSize(n int) |
derived | Sets job/result channel buffer in unordered mode, or max in-flight window (max(workers*2, 16) default) in ordered mode. |
Serial Fast-Path (Workers(1))¶
When ParMap resolves to Workers(1) and BufferSize is not set, Automi bypasses channel allocation and goroutine creation completely and runs fn inline inside the pull iterator.
// Runs inline with zero goroutines when cfg.Workers == 1,
// or launches cfg.Workers goroutines when cfg.Workers > 1.
out := stream.ParMap(step, automi.Workers(cfg.Workers), automi.Ordered())
To keep asynchronous channel decoupling with a single worker, pass BufferSize(n) alongside Workers(1):
Ordering and Memory Bounds¶
- Unordered (default): Emits each result as soon as its worker finishes, minimizing latency when per-item durations vary. Channel capacity is
BufferSizeif set, otherwiseworkers. - Ordered (
Ordered()): Caps total in-flight plus pending reordered items via a semaphore sized toBufferSizeif set, ormax(workers*2, 16)by default. Head-of-line stalls block upstream feeding cleanly once the window fills instead of growing maps unboundedly.
Errors, ErrSkip, and Panics¶
- Returning
ErrSkipdrops that item cleanly (even inOrdered()mode, advancing sequence numbers without stalling later items). - A non-skip error or recovered panic (
automi: worker panic: <val>) cancels sibling workers, awaits full goroutine teardown, and halts the stream with the error. - Parent context cancellation propagates cleanly even if a worker handles
<-ctx.Done()by returningErrSkip.
ParEach — Concurrent Terminal Side Effects¶
(s Stream[T]) ParEach(ctx context.Context, workers int, fn func(context.Context, T) error) error runs a fallible side-effect function across a bounded worker pool and blocks until all items finish, an error occurs, or ctx cancels.
err := automi.FromSlice(webhooks).
ParEach(ctx, 8, func(ctx context.Context, url string) error {
return sendWebhook(ctx, url)
})
Behavior and Contracts¶
- Worker count:
workers <= 0defaults tomax(runtime.GOMAXPROCS(0), 1).workers == 1executes inline without goroutines or channels. - Nil callback:
fn == nilreturnsErrNilFunc. ErrSkip: Ignored without failing the stream.- Errors & panics: The first non-skip error, recovered worker panic (
automi: worker panic: <val>), or upstream stream error cancels sibling workers, waits for all workers to return (sync.WaitGroup.Wait()), and returns that first error. - Cancellation: If workers return
ErrSkipon shutdown after parentctxcancels,ParEachstill returnsctx.Err().
Use ParMap when downstream stages need transformed outputs; use ParEach when the pipeline ends with side effects such as database writes, RPC calls, or broker publishes.