Skip to content

Stream Combinators

Combinators join multiple streams into one (Concat, Merge, Zip) or broadcast one stream into multiple destinations (FanOut). All four are package-level generic functions in automi.

Three vignettes illustrating Merge combining two streams into one, Zip pairing two streams like zipper teeth, and FanOut splitting one stream into three sinks

See runnable combinator pipelines in Multi-Region Log Pipeline (Merge), Alerts, FanOut & ParEach (FanOut), and Stream Reconciliation (Zip).

Concat — Sequential Concatenation

Concat[T any](streams ...Stream[T]) Stream[T] emits every item from the first stream before starting the next, in argument order.

  • Zero streams returns an empty stream; one stream returns it directly.
  • Checks ctx.Err() before starting each subsequent stream and stops immediately on the first upstream error.
all := automi.Concat(backfillStream, liveTailStream)

Merge — Concurrent Fan-In

Merge[T any](streams ...Stream[T]) Stream[T] reads all input streams concurrently and yields elements as they arrive on a single output stream.

flowchart LR
    A["Stream A"] --> M["Merge"]
    B["Stream B"] --> M
    C["Stream C"] --> M
    M --> Out["Single Stream[T]"]
  • Launches one producer goroutine per input stream writing into a shared bounded channel (cap = len(streams)).
  • Any upstream error, consumer error, or early termination (break in for range, or Take(n)) cancels the child context and waits for every upstream producer goroutine to exit before returning.
  • Zero streams returns an empty stream; one stream returns it unchanged with no goroutines.
combined := automi.Merge(
    automi.FromChan(regionA),
    automi.FromChan(regionB),
)

Zip — Lockstep Pairing

Zip[A, B any](a Stream[A], b Stream[B]) Stream[Pair[A, B]] pulls one element from a and one from b using iter.Pull2, yielding Pair[A, B]{First, Second} values by position.

type Pair[A, B any] struct {
    First  A
    Second B
}
  • Stops immediately when the shorter stream ends or either stream returns an error.
  • Cancels its child context before stopping both pull iterators so a blocked longer stream (for instance FromChan) unblocks and cleans up without leaking goroutines.
err := automi.Zip(automi.FromSeq(users), automi.FromChan(scores)).
    Map(func(p automi.Pair[User, Score]) string {
        return fmt.Sprintf("%s=%d", p.First.ID, p.Second.Val)
    }).
    Into(ctx, automi.ToWriter[string](out))

FanOut — Concurrent Multi-Sink Broadcast

FanOut[T any](ctx context.Context, s Stream[T], sinks ...Sink[T]) error delivers every stream element concurrently to all provided sinks.

  • Validates every sink up front; any nil sink returns ErrNilSink.
  • Zero sinks consumes and discards the stream via Discard[T](); one sink delegates directly to s.Into(ctx, sinks[0]).
  • Multiple sinks run in dedicated goroutines fed by bounded channels (cap 16 per sink).
  • An upstream error or an error from any sink cancels sibling sinks, shuts down channels cleanly, awaits all sink goroutines, and returns the primary error.
err := automi.FanOut(ctx, events,
    automi.ToFunc(writeDatabase),
    automi.ToChan(metricsCh, automi.CloseOnDone()),
)