Skip to content

Execution Model

Automi separates pipeline construction from pipeline execution. Building a Stream[T] allocates only closures; goroutines and channels start only when a terminal consumes an asynchronous stage.

Source dispensing tokens along a stream ribbon through a transform ring and filter sieve into a sink bowl

Pull-Based Lazy Blueprints

Every Stream[T] wraps an iterator factory func(context.Context) iter.Seq2[T, error]. Calling source constructors or transform methods returns a new immutable Stream[T] without reading data or starting background work.

A zero-value Stream[T]{} is valid and yields zero items with no error.

Execution begins when a terminal (Into, Collect, Each, ParEach, Reduce, GroupBy, All, or FanOut) passes a context.Context to pull elements upstream.

flowchart LR
    Src["Source / From*"] --> Sync["Fused Sync Stages<br/>(Filter, Map, TryMap, FlatMap)"]
    Sync --> Async["Async Boundary<br/>(ParMap / Batch / Buffer)"]
    Async --> Term["Terminal<br/>(Into / Collect / ParEach / All)"]

Synchronous Iterator Fusion

Sequential stages run inline inside the consumer's for v, err := range seq loop with zero extra goroutines and zero per-item channel hops:

  • Sources: FromSlice, FromSeq, FromSeq2, FromFunc, FromCSV, FromLines, FromReader, and From
  • Transforms: Filter, Take, Drop, Tap, Map, TryMap, FlatMap, Through
  • Size-only batching: Batch(s, MaxSize(n)) with no MaxWait
  • Serial fast-paths: ParMap configured with Workers(1) and no BufferSize, and ParEach with workers == 1

When a downstream stage stops (for example via Take(5) or break in a for range loop over All(ctx)), yield returns false and the pull unwinds upstream immediately.

Structured Concurrency at Async Boundaries

Stages that require concurrent execution, buffering, or clocks launch bounded goroutines scoped to a child context derived from the terminal's ctx:

  • ParMap (Workers > 1 or BufferSize > 0) and ParEach (workers > 1)
  • Batch with MaxWait > 0
  • Buffer and BufferDrop
  • Debounce
  • Merge, Zip, and FanOut

Every asynchronous stage enforces three invariants:

  1. Bounded channels: Internal queues are capped by worker count, window size, or explicit buffer capacity so slow consumers apply backpressure unless DropOldest() or DropNewest() is chosen.
  2. Sibling cancellation: The first non-ErrSkip error or recovered worker panic cancels the internal child context immediately.
  3. Complete teardown before return: Each stage waits for its producer and worker goroutines (sync.WaitGroup.Wait()) before returning from Into/ParEach or exiting its iter.Seq2 function—even if a caller exits a for range s.All(ctx) loop early.

Why Batch, Zip, Concat, Merge, and FanOut Are Functions

Go 1.27 supports generic methods on concrete types (func (s Stream[T]) Map[U any](...) Stream[U]), which covers transforms like Map, TryMap, ParMap, FlatMap, Through, Reduce, and GroupBy, plus Step.Then.

However, Go's type checker forbids methods on a generic type Stream[T] whose signature instantiates Stream with a type parameter that wraps T (such as Stream[[]T] or Stream[Pair[T, U]]), because recursive instantiation checks reject type growth on methods. For that reason:

  • Batch[T] (Stream[T] -> Stream[[]T]) and Zip[A, B] (Stream[Pair[A, B]]) are package-level functions.
  • Concat[T], Merge[T], and FanOut[T] are package-level variadic combinators.

Because Batch, Zip, Concat, and Merge still return Stream[...], you continue chaining methods directly on their result:

err := automi.Zip(users, scores).
    Map(formatPair).
    ParMap(uploadPair, automi.Workers(4)).
    Into(ctx, sink)