demux

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package demux splits the values received from one channel by key.

ByKey starts a consumer for each key it sees and gives it the values with that key, in order. Consumers of different keys run concurrently; values of one key are never processed concurrently.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByKey

func ByKey[T any, K comparable](ctx context.Context, in <-chan T, key func(T) K, consume func(K, <-chan T), opts ...Option) error

ByKey routes values received from in to consumers it starts for each key.

The first value with a new key starts a goroutine running consume(key, ch). Channel ch delivers that value and every later value with the same key, in the order they were received. A key's consumers never run at the same time: when a key closed by WithMaxKeys comes back, its new consumer starts only after the previous one has returned.

A consumer may return before ch is closed, for example after an error. The values of its key that are left, or still to come, are then discarded until the key is closed, so the other keys keep flowing.

ByKey runs in the caller's goroutine until in is closed or ctx is done. It then closes every channel, waits for every consumer to return, and returns ctx.Err() if ctx ended it, or nil otherwise. Values already buffered in a channel are still delivered.

Values are sent one at a time, so a consumer that falls behind holds up the others. WithBuffer gives each consumer room to absorb short delays.

Example
package main

import (
	"context"
	"fmt"
	"maps"
	"slices"
	"sync"

	"github.com/floatdrop/chan/demux"
)

func main() {
	type order struct{ customer, item string }

	in := make(chan order, 5)
	for _, o := range []order{
		{"bob", "tea"}, {"alice", "cake"}, {"bob", "milk"}, {"carol", "tea"}, {"alice", "jam"},
	} {
		in <- o
	}
	close(in)

	var (
		mu    sync.Mutex
		items = make(map[string][]string)
	)
	customer := func(o order) string { return o.customer }
	// Each customer's orders are handled in their own goroutine, in order.
	handle := func(name string, orders <-chan order) {
		var got []string
		for o := range orders {
			got = append(got, o.item)
		}
		mu.Lock()
		items[name] = got
		mu.Unlock()
	}

	if err := demux.ByKey(context.Background(), in, customer, handle, demux.WithBuffer(8)); err != nil {
		panic(err)
	}

	// ByKey has waited for every handler to return.
	for _, name := range slices.Sorted(maps.Keys(items)) {
		fmt.Println(name, items[name])
	}
}
Output:
alice [cake jam]
bob [tea milk]
carol [tea]

Types

type Option

type Option func(*config)

Option configures ByKey.

func WithBuffer

func WithBuffer(n int) Option

WithBuffer sets the capacity of each key's channel. The default is 0: the channels are unbuffered. WithBuffer panics if n is negative.

func WithMaxKeys

func WithMaxKeys(n int) Option

WithMaxKeys limits ByKey to n open keys. When a value with a new key arrives and n keys are open, the least recently used key is closed: its channel is closed, so its consumer can finish. If that key comes back, a new consumer is started for it.

The default, 0, means no limit. WithMaxKeys panics if n is negative.

Jump to

Keyboard shortcuts

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